How to set up the base template path for a jersey in a Grizzly HTTP server?

With Apache Jersey and Grizzly http server, how do I customize a basic template template?

Since I do not use the Servlet container, I assign a base template path with an absolute file path. But Jersey answered 404.

Below 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 to create an HttpServer :

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

ServerConfiguration config = server.getServerConfiguration();
config.addHttpHandler(createJerseyHandler(), "/*");
+4
source share
1 answer

I misunderstood how Jersey finds a template file when resolving a template name.

With a resource com.example.app.Indexand a base template path/templates

Relative template reference @Template(name = "index")

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

@Template(name = "/index")

/templates/index.mustache

.  : 19.3.

+7

Source: https://habr.com/ru/post/1538204/


All Articles