How to run jersey-server webservice server without using tomcat

前端 未结 3 1187
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 14:12

This is my first time dealing with web-services. Simply, I need to send a post request from jersey web service client (inside a webpage implemented in javascript) to a jerse

3条回答
  •  [愿得一人]
    2020-12-24 15:06

    You don't have to run a Jersey app in an installed web server. You can run it in an embedded server, meaning a server that runs in standalone mode with a normal main method.

    If you are using Maven, and you are familiar with creating Maven archetypes, you can use the jersey-quickstart-grizzly2 archetype

    • From Command line
    • From Eclipse (except use jersey-quickstart-grizzly2)
    • From Netbeans (See bottom of answer. Also use jersey-quickstart-grizzly2).

    This is everything you get for free with the archetype project.

    enter image description here

    Main.java

    package com.underdog.jersey.grizzly;
    
    import org.glassfish.grizzly.http.server.HttpServer;
    import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
    import org.glassfish.jersey.server.ResourceConfig;
    
    import java.io.IOException;
    import java.net.URI;
    
    /**
     * Main class.
     *
     */
    public class Main {
        // Base URI the Grizzly HTTP server will listen on
        public static final String BASE_URI = "http://localhost:8080/myapp/";
    
        /**
         * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
         * @return Grizzly HTTP server.
         */
        public static HttpServer startServer() {
            // create a resource config that scans for JAX-RS resources and providers
            // in com.underdog.jersey.grizzly package
            final ResourceConfig rc = new ResourceConfig().packages("com.underdog.jersey.grizzly");
    
            // create and start a new instance of grizzly http server
            // exposing the Jersey application at BASE_URI
            return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
        }
    
        /**
         * Main method.
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            final HttpServer server = startServer();
            System.out.println(String.format("Jersey app started with WADL available at "
                    + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
            System.in.read();
            server.stop();
        }
    }
    

    MyResource.java

    package com.underdog.jersey.grizzly;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    /**
     * Root resource (exposed at "myresource" path)
     */
    @Path("myresource")
    public class MyResource {
    
        /**
         * Method handling HTTP GET requests. The returned object will be sent
         * to the client as "text/plain" media type.
         *
         * @return String that will be returned as a text/plain response.
         */
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String getIt() {
            return "Got it!";
        }
    }
    

    MyResourceTest.java

    package com.underdog.jersey.grizzly;
    
    import javax.ws.rs.client.Client;
    import javax.ws.rs.client.ClientBuilder;
    import javax.ws.rs.client.WebTarget;
    
    import org.glassfish.grizzly.http.server.HttpServer;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import static org.junit.Assert.assertEquals;
    
    public class MyResourceTest {
    
        private HttpServer server;
        private WebTarget target;
    
        @Before
        public void setUp() throws Exception {
            // start the server
            server = Main.startServer();
            // create the client
            Client c = ClientBuilder.newClient();
    
            // uncomment the following line if you want to enable
            // support for JSON in the client (you also have to uncomment
            // dependency on jersey-media-json module in pom.xml and Main.startServer())
            // --
            // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());
    
            target = c.target(Main.BASE_URI);
        }
    
        @After
        public void tearDown() throws Exception {
            server.stop();
        }
    
        /**
         * Test to see that the message "Got it!" is sent in the response.
         */
        @Test
        public void testGetIt() {
            String responseMsg = target.path("myresource").request().get(String.class);
            assertEquals("Got it!", responseMsg);
        }
    }
    

    pom.xml - I added the jersey-media-json-jackson and the maven-assembly-plugin myself, so that you can create a single runnable jar file.

    
    
        4.0.0
    
        com.underdog
        jersey-grizzly
        jar
        1.0-SNAPSHOT
        jersey-grizzly
    
        
            
                
                    org.glassfish.jersey
                    jersey-bom
                    ${jersey.version}
                    pom
                    import
                
            
        
    
        
            
                org.glassfish.jersey.containers
                jersey-container-grizzly2-http
            
             
                org.glassfish.jersey.media
                jersey-media-json-jackson
            
            
                junit
                junit
                4.9
                test
            
        
    
        
            ${project.artifactId}
            
                
                    maven-assembly-plugin
                    2.5.3
                    
                        
                            jar-with-dependencies
                        
                        
                            
                                com.underdog.jersey.grizzly.Main
                            
                        
                    
                    
                        
                            create-archive
                            package
                            
                                single
                            
                        
                    
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    2.5.1
                    true
                    
                        1.7
                        1.7
                    
                
                
                    org.codehaus.mojo
                    exec-maven-plugin
                    1.2.1
                    
                        
                            
                                java
                            
                        
                    
                    
                        com.underdog.jersey.grizzly.Main
                    
                
            
        
    
        
            2.17
            UTF-8
        
    
    

    With all the above, you can cd to the project from the command line and do

    1. mvn clean package
    2. java -jar target/jersey-grizzly-jar-with-dependencies.jar

    and the application will start.

    You can access it from http://localhost:8080/myapp/myresource

    That's it. Note that the above is a normal jar project. So if you can't follow how to create the archetype, you can pretty much copy everything above into a jar project.

    See Also:

    • Getting Started with Jersey Using Maven for some more explanation.

提交回复
热议问题