【问题标题】:Java Jersey 2: standalone?Java Jersey 2:独立?
【发布时间】:2018-06-21 16:05:59
【问题描述】:

是否可以在 Java 应用程序中独立使用 Jersey REST 服务? 我找到的每个示例都在 web 容器 (web.xml) 的上下文中。

感谢您的帮助!

【问题讨论】:

    标签: java jersey


    【解决方案1】:

    只需关注Jersey docs getting started。它使用 Grizzly 服务器创建一个独立的服务器。使用 Maven,您可以使用以下原型命令轻松创建它。

    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 中。找出那些不在捆绑包中的并搜索那些。

    我认为不包含在 RI 捆绑包 DL 中的是

    • grizzly 框架
    • grizzly-http
    • grizzly-http-server
    • jersey-container-grizzly2-http

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

    com.example.MyResource

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

    【讨论】:

    • 我会用灰熊试试。据我所知,它不在最新的球衣包(jaxrs-ri-2.27.zip)中。我刚刚通过谷歌搜索找到了 jars,但我认为那里有一些版本冲突。灰熊在某个地方有官方包装吗?我不使用maven,所以有点手工。
    • 我更新了我的答案以显示所有由原型拉入的罐子,并发布了所有生成的类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2017-04-28
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多