java - Registering Thymeleaf Templateengine in Jersey -


i'm trying combine thymeleaf template engine jersey rest server.

i found example of thymeleafviewprocessor (https://github.com/bufferings/jersey-thymeleaf) don't understand how bind processor jersey server.

@provider public class thymeleafviewprocessor implements viewprocessor<string> {   @context   servletcontext servletcontext;    @context   threadlocal<httpservletrequest> requestinvoker;    @context   threadlocal<httpservletresponse> responseinvoker;    templateengine templateengine;    public thymeleafviewprocessor() {     templateresolver templateresolver = new servletcontexttemplateresolver();     templateresolver.setprefix("/web-inf/templates/");     templateresolver.setsuffix(".html");     templateresolver.settemplatemode("html5");     templateresolver.setcachettlms(3600000l);      templateengine = new templateengine();     templateengine.settemplateresolver(templateresolver);   }    @override   public string resolve(final string path) {     return path;   }    @override   public void writeto(final string resolvedpath, final viewable viewable,       final outputstream out) throws ioexception {     // commit status , headers httpservletresponse     out.flush();      webcontext context = new webcontext(requestinvoker.get(),         responseinvoker.get(), servletcontext, requestinvoker.get().getlocale());     map<string, object> variables = new hashmap<>();     variables.put("it", viewable.getmodel());     context.setvariables(variables);     templateengine.process(viewable.gettemplatename(), context, responseinvoker         .get().getwriter());   } 

here start class:

public class app {    /**    * starts lightweight http server serving jax-rs application.    *    * @return new instance of lightweight http server    * @throws ioexception    */   static httpserver startserver() throws ioexception {     // create new server listening @ port 8080     httpserver server = httpserver.create(new inetsocketaddress(getbaseuri().getport()), 0);      // create handler wrapping jax-rs application     httphandler handler = runtimedelegate.getinstance().createendpoint(new jaxrsapplication(), httphandler.class);      // map jax-rs handler server root     server.createcontext(getbaseuri().getpath(), handler);      // start server     server.start();      return server;   }    @suppresswarnings("resultofmethodcallignored")   public static void main(string[] args) throws ioexception {     system.out.println("\"hello world\" jersey example application");      httpserver server = startserver();      system.out.println("application started.\n"         + "try accessing " + getbaseuri() + "hello in browser.\n"         + "hit enter stop application...");     system.in.read();     server.stop(0);   }    private static int getport(int defaultport) {     final string port = system.getproperty("jersey.config.test.container.port");     if (null != port) {       try {         return integer.parseint(port);       } catch (numberformatexception e) {         system.out.println("value of jersey.config.test.container.port property"             + " not valid positive integer [" + port + "]."             + " reverting default [" + defaultport + "].");       }     }     return defaultport;   }    /**    * gets base {@link uri}.    *    * @return base {@link uri}.    */   public static uri getbaseuri() {     return uribuilder.fromuri("http://localhost/").port(getport(8080)).build();   } } 

helloresource:

@path("/hello") public class helloresource {    @get   @path("/{n}")   @produces(mediatype.text_html)   public viewable sayhello(@pathparam("n") string name) {     return new viewable("sample", "hello " + name + "!");   }    @get   @produces(mediatype.text_html)   public viewable sayhello() {     return new viewable("sample",         new samplemodel("good morning", "my friends"));   }    public static class samplemodel {     public string greeting;     public string name;      public samplemodel(string greeting, string name) {       this.greeting = greeting;       this.name = name;     }   } } 

what have bind viewprocessor jersey. don't find examples in web , other solutions (freemarker) seem work differently.

i solved binding view processor eager singleton:

public class templatemodule extends abstractmodule {     @override     protected void configure() {         // bind thymeleaf processor eager singleton         bind(thymeleafviewprocessor.class).aseagersingleton();     } } 

then add injector. should work.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -