如何在Java SE环境中部署JAX-RS应用程序? [英] How to deploy a JAX-RS application on a Java SE environment?

查看:118
本文介绍了如何在Java SE环境中部署JAX-RS应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JAX-RS编写一个RESTful Web服务,我想像http://localhost:[port]一样在localhost上发布它.我在答案中阅读了以下内容:

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:

Java SE 7(JSR 336)和Java SE 8(JSR 337)规范 不要合并JAX-RS组件.但是,JAX-RS应用程序 可以在Java SE环境中发布(使用RuntimeDelegate)和 JAX-RS实现也可能支持通过JAX-WS发布.

The Java SE 7 (JSR 336) and the Java SE 8 (JSR 337) specifications don't incorporate the JAX-RS component. However, JAX-RS applications can be published in Java SE environments (using RuntimeDelegate) and JAX-RS implementations also may support publication via JAX-WS.

提到RuntimeDelegate.我该如何使用?如果有很好的例子说明如何完成任务,请与我分享.

The RuntimeDelegate is mentioned. How can I use it? If there are good examples on how to achieve it task, please share them with me.

推荐答案

要在Java SE环境中部署JAX-RS应用程序,可以使用

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.

在Java SE环境中,可以使用RuntimeDelegatecreateEndpoint方法获得端点类的已配置实例.该应用程序提供Application的实例和所需端点的类型.一个实现可以支持零个或多个任何所需类型的端点类型.

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(JAX-RS参考实现)支持范围为HTTP服务器,可用于在Java SE中部署JAX-RS应用程序.

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

例如,使用 Grizzly

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.";
        }
    }
}

该应用程序将在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文档还描述了没有 RuntimeDelegate .

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

这篇关于如何在Java SE环境中部署JAX-RS应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆