我想用JAX-RS编写RESTful Web服务,我想在http://localhost:[port]
这样的localhost上发布它。我在此answer中阅读了以下内容:
Java SE 7(JSR 336)和Java SE 8(JSR 337)规范 不要合并JAX-RS组件。但是,JAX-RS应用程序 可以在Java SE环境中发布(使用
RuntimeDelegate
)和 JAX-RS实现也可以通过JAX-WS支持发布。
提到了RuntimeDelegate
。我怎么用呢?如果有关于如何完成任务的好例子,请与我分享。
答案 0 :(得分:4)
要在Java SE环境中部署JAX-RS应用程序,可以使用RuntimeDelegate
和JAX-RS实现支持的HTTP服务器。不需要servlet容器。
JSR 339声明如下:
在Java SE环境中,可以使用
createEndpoint
的{{1}}方法获取已配置的端点类实例。该应用程序提供RuntimeDelegate
的实例以及所需的端点类型。实现可以支持任何所需类型的零个或多个端点类型。如何使用生成的端点类实例来发布应用程序超出了本规范的范围。
Jersey,JAX-RS参考实现,支持range of HTTP servers,您可以使用它在Java SE中部署JAX-RS应用程序。
例如,使用Grizzly和RuntimeDelegate
,您可以拥有以下内容:
Application
该应用程序将在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
其他支持的实施包括:
Jersey documentation还描述了没有RuntimeDelegate
的Java SE环境的其他部署选择。