title: Registering Thymeleaf Templateengine in Jersey
body: <p>I'm trying to combine the Thymeleaf template engine with a Jersey REST Server.</p>
  
  <p>I already found this example of a ThymeleafViewProcessor (<a href="https://github.com/bufferings/jersey-thymeleaf" rel="nofollow">https://github.com/bufferings/jersey-thymeleaf</a>) but I don't understand how to bind the processor to the jersey server.</p>
  
  <pre><code>@Provider
  public class ThymeleafViewProcessor implements ViewProcessor&lt;String&gt; {
    @Context
    ServletContext servletContext;
  
    @Context
    ThreadLocal&lt;HttpServletRequest&gt; requestInvoker;
  
    @Context
    ThreadLocal&lt;HttpServletResponse&gt; 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 the status and headers to the HttpServletResponse
      out.flush();
  
      WebContext context = new WebContext(requestInvoker.get(),
          responseInvoker.get(), servletContext, requestInvoker.get().getLocale());
      Map&lt;String, Object&gt; variables = new HashMap&lt;&gt;();
      variables.put("it", viewable.getModel());
      context.setVariables(variables);
      templateEngine.process(viewable.getTemplateName(), context, responseInvoker
          .get().getWriter());
    }
  </code></pre>
  
  <p>Here is my start up class:</p>
  
  <pre><code>public class App {
  
    /**
     * Starts the lightweight HTTP server serving the JAX-RS application.
     *
     * @return new instance of the lightweight HTTP server
     * @throws IOException
     */
    static HttpServer startServer() throws IOException {
      // create a new server listening at port 8080
      HttpServer server = HttpServer.create(new InetSocketAddress(getBaseURI().getPort()), 0);
  
      // create a handler wrapping the JAX-RS application
      HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new JaxRsApplication(), HttpHandler.class);
  
      // map JAX-RS handler to the server root
      server.createContext(getBaseURI().getPath(), handler);
  
      // start the 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 the browser.\n"
          + "Hit enter to stop the 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"
              + " is not a valid positive integer [" + port + "]."
              + " Reverting to 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();
    }
  }
  </code></pre>
  
  <p>HelloResource:</p>
  
  <pre><code>@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;
      }
    }
  }
  </code></pre>
  
  <p>What do I have to do now to bind the ViewProcessor to Jersey. I don't find any examples in the web and other solutions (freemarker) seem to work differently.</p>