Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup template base path for Jersey in Grizzly http server?

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(), "/*");
like image 861
Rangi Lin Avatar asked Apr 26 '14 16:04

Rangi Lin


1 Answers

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

like image 156
Rangi Lin Avatar answered Oct 12 '22 22:10

Rangi Lin