How to deploy a JAX-RS application on a Java SE environment?

前端 未结 1 588
Happy的楠姐
Happy的楠姐 2021-01-19 16:49

I want to code a RESTful web service with JAX-RS, and I want publish it on localhost like http://localhost:[port]. I read the following in this answer:

相关标签:
1条回答
  • 2021-01-19 17:08

    To deploy a JAX-RS application in a Java SE environment, you could use RuntimeDelegate and a HTTP server supported by your JAX-RS implementation. A servlet container is not required.

    The JSR 339 states the following:

    In a Java SE environment a configured instance of an endpoint class can be obtained using the createEndpoint method of RuntimeDelegate. The application supplies an instance of Application and the type of endpoint required. An implementation MAY support zero or more endpoint types of any desired type.

    How the resulting endpoint class instance is used to publish the application is outside the scope of this specification.

    Jersey, the JAX-RS reference implementation, supports a range of HTTP servers which you can use to deploy JAX-RS applications in Java SE.

    For example, with Grizzly and RuntimeDelegate, you can have the following:

    public class Example {
    
        public static void main(String[] args) {
    
            ResourceConfig resourceConfig = new ResourceConfig();
            resourceConfig.register(GreetingsResource.class);
    
            HttpHandler handler = RuntimeDelegate.getInstance()
                    .createEndpoint(resourceConfig, HttpHandler.class);
    
            HttpServer server = HttpServer.createSimpleServer(null, 8080);
            server.getServerConfiguration().addHttpHandler(handler);
    
            try {
                server.start();
                System.out.println("Press any key to stop the server...");
                System.in.read();
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    
        @Path("/greetings")
        public static class GreetingsResource {
    
            @GET
            @Produces(MediaType.TEXT_PLAIN)
            public String getGreeting(){
                return "Hello from the other side.";
            }
        }
    }
    

    The application will be available at http://localhost:8080/greetings.

    The following dependencies are required for the example shown above:

    <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-http-server</artifactId>
        <version>2.3.30</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
        <version>2.25.1</version>
    </dependency>
    

    Other supported implementations include:

    • JDK HTTP Server
    • Simple Server
    • Jetty HTTP Server
    • Netty HTTP Server

    Jersey documentation also describes other deployment alternatives for a Java SE environment without RuntimeDelegate.

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