gpt4 book ai didi

java - 如何在 Java SE 环境中部署 JAX-RS 应用程序?

转载 作者:搜寻专家 更新时间:2023-11-01 02:37:41 24 4
gpt4 key购买 nike

我想用 JAX-RS 编写一个 RESTful web 服务,我想在本地主机上发布它,比如 http://localhost:[port]。我在这个 answer 中阅读了以下内容:

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。我该如何使用它?如果有关于如何完成任务的好例子,请与我分享。

最佳答案

要在 Java SE 环境中部署 JAX-RS 应用程序,您可以使用 RuntimeDelegate以及您的 JAX-RS 实现支持的 HTTP 服务器。不需要 servlet 容器。

JSR 339陈述如下:

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 引用实现,支持 range of HTTP servers您可以使用它在 Java SE 中部署 JAX-RS 应用程序。

例如,GrizzlyRuntimeDelegate ,你可以有以下内容:

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 上可用。

上面显示的示例需要以下依赖项:

<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>

其他支持的实现包括:

Jersey documentation还描述了没有 RuntimeDelegate 的 Java SE 环境的其他部署方案。 .

关于java - 如何在 Java SE 环境中部署 JAX-RS 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43278950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com