java官方 jax rs,如何在Java SE环境中部署JAX-RS应用程序?

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:

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.

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.

解决方案

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:

org.glassfish.grizzly

grizzly-http-server

2.3.30

org.glassfish.jersey.containers

jersey-container-grizzly2-http

2.25.1

Other supported implementations include:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值