Glassfish Jersey MVC Hello World の続き
package com.mycompany.jerseymvcexam; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.mvc.mustache.MustacheMvcFeature; @javax.ws.rs.ApplicationPath("webresources") public class ApplicationConfig extends ResourceConfig { public ApplicationConfig() { this .packages(ApplicationConfig.class.getPackage().getName()) .property(MustacheMvcFeature.TEMPLATE_BASE_PATH, "/Users/atsushi/templates") .register(MustacheMvcFeature.class); } }
@javax.ws.rs.ApplicationPath("webresources") public class ApplicationConfig extends Application { static class MyFeature implements Feature { @Override public boolean configure(FeatureContext fc) { fc.property(MustacheMvcFeature.TEMPLATE_BASE_PATH, "/Users/atsushi/templates"); return true; } } @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new java.util.HashSet<>(); // Jersey MVC resources.add(MyFeature.class); resources.add(MustacheMvcFeature.class); // Rest Resource Classes addRestResourceClasses(resources); return resources; } /** * Do not modify addRestResourceClasses() method. * It is automatically populated with * all resources defined in the project. * If required, comment out calling this method in getClasses(). */ private void addRestResourceClasses(Set<Class<?>> resources) { resources.add(GenericResource.class); } }
key (constant field) | key (string) | mean |
MustacheMvcFeature?.TEMPLATE_BASE_PATH | "jersey.config.server.mvc.templateBasePath?.mustache" | The base path where Mustache templates are located. |
MustacheMvcFeature?.CACHE_TEMPLATES | "jersey.config.server.mvc.caching.mustache" | Enables caching of Mustache templates to avoid multiple compilation. |
MustacheMvcFeature?.TEMPLATE_OBJECT_FACTORY | "jersey.config.server.mvc.factory.mustache" | Property used to pass user-configured MustacheFactory?. |
MustacheMvcFeature?.ENCODING | "jersey.config.server.mvc.encoding.mustache" | Property used to configure a default encoding that will be used if none is specified in @Produces annotation. If property is not defined the UTF-8 encoding will be used as a default value. |
@Path("generic") public class GenericResource { ... @GET @Path("hello") @Produces(MediaType.TEXT_HTML) @Template(name = "/hello1") public Person hello() { return new Person("Ms.","Yuko"); } ... }
<!DOCTYPE html> <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h1>Good evening {{title}} {{name}}</h1> </body> </html>