Question

With Apache Jersey and Grizzly http server, how do I setup base template path ?

Since I'm not using Servlet container, I assign template base path with absolute file path. But Jersey response a 404.

Following is my project setup

Project Directory :

src
 └─ java
      .....
 └─ resources
    └─ templates
       └─ index.mustache

Application :

public class ExampleApplication extends ResourceConfig {

  public CustomTableApplication() {
    packages("com.example.app");

    setupTemplateEngine();
  }

  private void setupTemplateEngine() {
    property(MvcFeature.TEMPLATE_BASE_PATH, "/templates/");
    register(MustacheMvcFeature.class);
  }
}

Controller :

@Path("/")
public class Index {

  @GET
  @Template(name = "index")
  public String index() {
    return "";
  }
}

How I create HttpServer :

HttpServer server = new HttpServer();
NetworkListener listener = new NetworkListener("example", "localhost", 8080);
server.addListener(listener);

ServerConfiguration config = server.getServerConfiguration();
config.addHttpHandler(createJerseyHandler(), "/*");
Was it helpful?

Solution

I misunderstood how Jersey find template file when resolving template name.

With resource com.example.app.Index and template base path /templates

Relative Template Reference @Template(name = "index")

/templates/com/example/app/Index/index.mustache

Absolute Template Reference @Template(name = "/index")

/templates/index.mustache

see document for more detail : 19.3. Absolute vs. Relative template reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top