Anton Sarov
Anton Sarov

Reputation: 3748

using mustache templates with jersey

I want to integrate Mustache-based templates with Jersey 2.

In my pom.xml I have:

<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-mustache</artifactId>
<version>2.9</version>
</dependency>

My resource class looks like this:

@Path(value = "/appstatus")
public class AppStatusChecker
{

    @Template(name = "/index.mustache")
    @GET
    public Context getStatus() {
        return new Context(4);
    }

    public static class Context {
        public Integer value;

        public Context(final Integer value) {
           this.value = value;
        }
    }
}

In my web.xml I have this:

<init-param>
    <param-name>jersey.config.server.mvc.templateBasepath.mustache</param-name>
    <param-value>/templates</param-value>
</init-param>

And when the app is deployed, under WEB-INF/classes I have a folder templates with index.mustache inside. The content is:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Simple mustache test</title>
  </head>
  <body>
    <h1>This is mustacheee</h1>
    {{value}}
  </body>
</html>

What I see after calling the myapp/appstatus URL is: {"value":4} but I would expect some HTML. Is there some important part of the setup I am missing?

Upvotes: 3

Views: 2295

Answers (1)

Skylar Saveland
Skylar Saveland

Reputation: 11444

I had to register my provider like:

    final ResourceConfig rc = new ResourceConfig().property(
            MustacheMvcFeature.TEMPLATE_BASE_PATH, "templates"
    ).register(
            MustacheMvcFeature.class
    ).packages("com.example");

    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);

Not sure if you are using Grizzly. But, maybe this will help someone.

https://jersey.java.net/documentation/latest/mvc.html#mvc.registration

Upvotes: 4

Related Questions