How do I update example to work with latest versions Jetty (9.1.0.RC2) and Jersey (2.7)?

前端 未结 1 424
醉梦人生
醉梦人生 2020-12-10 08:56

I\'ve been trying to follow this example (first hit on google for jersey + jetty) but with not much luck.

At the suggestion of the commenters below, I\'ve decided to

1条回答
  •  有刺的猬
    2020-12-10 09:47

    I realise that this isn't getting the example you gave to work (your example link is broken) - I don't know Jersey 1 very well, and trying to upgrade someone else's project is difficult. As you have another question asking for a HelloWorld example, I assume you're just needing something to get yourself going with Jersey & Jetty.

    So here you go - 2 examples, one using the JettyHttpContainerFactory and the other using the Jersey ServletContainer.

    First the Jersey Resource - really simple. This sets the class with a path of "test", and one method with a path of hello, accepting GET which produces "Hello World" in plain text.

    @Path("/test")
    public class TestResource {
    
        @GET
        @Path("hello")
        @Produces(MediaType.TEXT_PLAIN)
        public String hello() {
           return "Hello World";
        }
    }
    

    Next the server class:

    public class ExampleServer {
    
        public static void main(String[] args) {
    
                URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
                ResourceConfig config = new ResourceConfig(TestResource.class);
                Server server = JettyHttpContainerFactory.createServer(baseUri, config);
           }
    }
    

    And finally the pom dependencies (note there are dependencies here for both examples).

           
                
                    org.eclipse.jetty
                    jetty-server
                    9.1.3.v20140225
                
                
                    org.eclipse.jetty
                    jetty-servlet
                    9.1.3.v20140225
                
                
                    org.glassfish.jersey.core
                    jersey-server
                    2.7
                
                
                    org.glassfish.jersey.containers
                    jersey-container-servlet-core
                    2.7
                
                
                    org.glassfish.jersey.containers
                    jersey-container-jetty-http
                    2.7
                
                
                    org.glassfish.jersey.media 
                    jersey-media-moxy 
                    2.7 
                
                
    
          
    

    Also see https://jersey.java.net/apidocs/2.7/jersey/javax/ws/rs/core/Feature.html for an understanding of Features - by including Moxy on the classpath, Jersey will automatically register the MoxyJSONFeature. If you'd rather use Jackson, you'll need to manually register the JacksonFeature, as well as the dependency. You can register any feature, in the same init param as registering your resources (comma separated)

    If you would prefer to configure as a servlet use this as the ExampleServer code

    public class ExampleServer {
    
        public static void main(String[] args) throws Exception {
    
                Server server = new Server(9998);
    
                ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
                context.setContextPath("/");
    
                server.setHandler(context);
    
                ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
                jerseyServlet.setInitOrder(0);
    
                /*This parameter tells the Jersey Servlet which of your REST resources to load. In this example we're adding the TestResource class. Jersey will then invoke this class for requests coming into paths denoted by the @Path parameter within the TestResource class. If you have multiple classes, you can either list them all comma separated, of use "jersey.config.server.provider.packages" and list the package name instead */
                jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "foo.bar.TestResource");
                server.start();
                server.join();
           }
    }
    

    Note with the servlet version, I'm defining the class name of my resource. If you have a few it's best to provide the package name instead using jersey.config.server.provider.packages

    Hope this helps. Let me know if you have any problems.

    Will

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