Jersey: How to Add Jackson to Servlet Holder

前端 未结 1 1777
悲哀的现实
悲哀的现实 2020-12-03 18:48

I am creating an embedded Jetty webapp with Jersey. I do not know how to add Jackson for automatic JSON serde here:

    ServletHolder jerseyServlet = contex         


        
1条回答
  •  情书的邮戳
    2020-12-03 19:22

    One way is to just wrap the ResourceConfig in an explicit construction of the ServletContainer, as seen here.

    Tested with your example

    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();
            }
        }
    }
    

    You could also...

    without changing anything else in your original post, just set the init param to scan the Jackson provider package

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

    Note your attempted use of ResourceConfig seems a little redundant, as you are already configuring your classes in the the init param. You could alternatively get rid of adding each class explicitly and just scan entire packages as I have done.

    You could also...

    just use the Jackson provider classes you need. You can look in the jar, and you will see more than just the marshalling/unmarhalling provider (Jackson[JAXB]JsonProvider), like a ExceptionMappers. You may not like these mappers and wand to configure your own. In which case, like I said, just include the provider you need. For example

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

    After further testing...

    Not sure what version of Jersey, but I am using Jersey 2.15 (with jersey-media-json-jackson:2.15), and without any further configuration from just scanning my package for my resource classes, the Jackson feature is already enabled. This is part of the auto discoverable features. I believe this was enable as of 2.8 or 2.9 for the Jackson feature. So if you are using a later one, I don't think you need to explicitly configure anything, at least from what I've tested :-)


    UPDATE

    All of the above examples have been tested with the below 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
                
            
        
    
    

    And resource class

    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;
        }
    }
    

    Using path

    http://localhost:9090/json

    0 讨论(0)
提交回复
热议问题