使用 JAX-RS 进行异步 REST 调用
我需要创建一个 RESTful 服务,它应该以以下方式支持异步调用。当用户调用某个方法时,他会获得 http“202”代码和 url 来轮询,在其中他可以看到请求的状态。目前我使用 JAX-RS 及其注释:
@Path("")
public interface MyService {
@POST
@Path("/myService/{name}")
@Consumes({APPLICATION_XML, APPLICATION_JSON})
void postSomething(@PathParam("name") String name, MyObject data);
}
此类映射将通过 url /myService/{name} 公开 MyService 的 postSomething() 方法,该方法为 POST 请求提供服务,从 url 获取“name”参数,从请求正文获取“data”。
我希望在发出此 PUT 请求后,客户端获得 202 http 代码和一些回调 url 来轮询以在执行方法后获取结果。
所以问题是: 1.如何让JAX-RS返回202代码? 2. 如何将回调url传递给客户端?
I need to create a RESTful service which should support async calls in follwing way. When user calls some method he got the http '202' code and url to poll where he can see the status of his request. Currently I use JAX-RS and its annoations:
@Path("")
public interface MyService {
@POST
@Path("/myService/{name}")
@Consumes({APPLICATION_XML, APPLICATION_JSON})
void postSomething(@PathParam("name") String name, MyObject data);
}
Such mapping would expose MyService's postSomething() method by url /myService/{name} which serves POST requests, get 'name' parameter from url and 'data' from request body.
I want that after making this PUT request client get 202 http code and some callback url to poll to get the result once method will be executed.
So the question is:
1. How to make JAX-RS return 202 code?
2. How to pass callback url to the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让 postSomething 方法返回一个 Response 对象:
如果您希望回调 URI 在 HTTP 正文中作为纯文本,您可以执行以下操作:
对于通过资源类生成 URI,请查看 UriBuilder
Have the postSomething method return a Response object:
If you want the callback URI as plain-text in the HTTP Body you could do something like this:
For generating URIs by resource classes, have a look at UriBuilder
使用@Context HttpServletResponse servletResponse 来直接控制servlet 的响应机制。
Use
@Context HttpServletResponse servletResponse
to get direct control over the servlet's response mechanism.