有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaJersey 2:独立?

是否可以在java应用程序中独立使用Jersey REST服务? 我发现的每个示例都在webcontainer(web.xml)的上下文中

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    只要跟着Jersey docs getting started走就行了。它使用Grizzly服务器创建一个独立的服务器。使用Maven,您可以使用以下archetype命令轻松创建它

    mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
    -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
    -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \
    -DarchetypeVersion=2.27
    

    最后一个参数-DarchetypeVersion可以替换为任意2。你想要的x版本。目前,最新版本为2.27

    运行此命令后,将生成一个从命令行运行的主类


    另一种方法是使用Spring Boot(很多人都在使用),您可以签出the official example和签出the docs


    更新

    如果您没有使用Maven,下面是使用原型的所有jar。大多数都包括在Jersey RI bundle you download from the docs中。只需找出那些不在捆绑包中的,然后搜索它们

    enter image description here

    我认为没有包含在RI包DL中的是

    • 网格框架
    • grizzly http
    • grizzly http服务器
    • jersey-container-grizzly2-http

    这里是从原型生成的所有类

    com。实例我的资源

    package com.example;
    
    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!";
        }
    }
    

    com。实例Main

    package com.example;
    
    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.example package
            final ResourceConfig rc = new ResourceConfig().packages("com.example");
    
            // 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();
        }
    }
    

    (测试)com。实例MyResourceTest

    package com.example;
    
    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);
        }
    }