泽西岛:如何将jackson添加到Servlet Holder

我正在使用Jersey创建一个嵌入式Jetty webapp。 我不知道如何在这里添加jackson自动JSON serde:

ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/*"); jerseyServlet.setInitOrder(0); jerseyServlet.setInitParameter( ServerProperties.PROVIDER_CLASSNAMES, StringUtils.join( Arrays.asList( HealthCheck.class.getCanonicalName(), Rest.class.getCanonicalName()), ";")); // Create JAX-RS application. final Application application = new ResourceConfig() .packages("com.example.application") .register(JacksonFeature.class); // what do I do now to tie this to the ServletHolder? 

如何在ServletHolder中注册此ResourceConfig ,以便在使用注释@Produces(MediaType.APPLICATION_JSON)使用Jackson? 这是嵌入式Jetty应用程序的完整主类

 package com.example.application.web; import com.example.application.api.HealthCheck; import com.example.application.api.Rest; import com.example.application.api.Frontend; import org.apache.commons.lang.StringUtils; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.ServerProperties; import javax.ws.rs.core.Application; import java.util.Arrays; public class JettyStarter { public static void main(String[] args) throws Exception { ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); Server jettyServer = new Server(9090); jettyServer.setHandler(context); ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/*"); jerseyServlet.setInitOrder(0); jerseyServlet.setInitParameter( ServerProperties.PROVIDER_CLASSNAMES, StringUtils.join( Arrays.asList( HealthCheck.class.getCanonicalName(), Rest.class.getCanonicalName()), ";")); // Create JAX-RS application. final Application application = new ResourceConfig() .packages("com.example.application") .register(JacksonFeature.class); try { jettyServer.start(); jettyServer.join(); } catch (Exception e) { System.out.println("Could not start server"); e.printStackTrace(); } finally { jettyServer.destroy(); } } } 

一种方法是将ResourceConfig包装在ServletContainer的显式构造中,如此处所示。

测试你的例子

 public class RestServer { public static void main(String[] args) throws Exception { // Create JAX-RS application. final ResourceConfig application = new ResourceConfig() .packages("jersey.jetty.embedded") .register(JacksonFeature.class); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); Server jettyServer = new Server(9090); jettyServer.setHandler(context); ServletHolder jerseyServlet = new ServletHolder(new org.glassfish.jersey.servlet.ServletContainer(application)); jerseyServlet.setInitOrder(0); context.addServlet(jerseyServlet, "/*"); // ... removed property (init-param) to compile. try { jettyServer.start(); jettyServer.join(); } catch (Exception e) { System.out.println("Could not start server"); e.printStackTrace(); } finally { jettyServer.destroy(); } } } 

你也可以……

如果不更改原始post中的任何其他内容,只需设置init参数即可扫描Jackson提供程序包

 jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "com.fasterxml.jackson.jaxrs.json;" + "jersey.jetty.embedded" // my package(s) ); 

请注意,您尝试使用ResourceConfig似乎有点多余,因为您已经在init参数中配置了类。 您也可以明确地删除每个类,并像我一样扫描整个包。

你也可以……

只需使用您需要的Jackson提供程序类。 您可以查看jar,您将看到的不仅仅是编组/解组提供程序(Jackson [JAXB] JsonProvider),就像ExceptionMappers一样。 您可能不喜欢这些映射器和魔杖配置您自己的。 在这种情况下,就像我说的那样,只需要包含您需要的提供程序。 例如

 jerseyServlet.setInitParameter(ServerProperties.PROVIDER_CLASSNAMES, "com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"); jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "jersey.jetty.embedded" // my package(s) ); 

经过进一步测试……

不知道什么版本的泽西岛,但我使用泽西2.15(使用jersey-media-json-jackson:2.15 ),并且没有任何进一步的配置只是扫描我的包我的资源类,jacksonfunction已经启用。 这是自动发现function的一部分。 我相信这是jacksonfunction的2.8或2.9。 因此,如果您使用的是后者,我认为您不需要明确配置任何内容,至少从我测试的内容开始:-)


UPDATE

所有上述示例都已使用以下Maven pom.xml进行了测试

   4.0.0 com.underdog.jersey jersey-jetty-embedded 1.0-SNAPSHOT jar  UTF-8 1.7 1.7 2.15 9.2.6.v20141205    org.glassfish.jersey.containers jersey-container-servlet   org.glassfish.jersey.media jersey-media-json-jackson   org.eclipse.jetty jetty-server ${jetty.version}   org.eclipse.jetty jetty-servlet ${jetty.version}   org.eclipse.jetty jetty-servlets ${jetty.version}      org.glassfish.jersey jersey-bom ${jersey.version} pom import     

和资源类

 import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @Path("/json") public class JsonResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getJson() { Resource resource = new Resource(); resource.hello = "world"; return Response.ok(resource).build(); } public static class Resource { public String hello; } } 

使用路径

http://localhost:9090/json