用JAX-RS异步REST调用 [英] Async REST calls with JAX-RS

查看:77
本文介绍了用JAX-RS异步REST调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个RESTful服务,该服务应以下列方式支持异步调用.当用户调用某种方法时,他获得了http'202'代码和url以进行轮询,以查看其请求状态.目前,我使用JAX-RS及其注释:

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

}

此类映射将通过服务POST请求的url/myService/{name}公开MyService的postSomething()方法,从url获取'name'参数,并从请求正文获取'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.

我希望在执行此PUT请求后,客户端获得202 http代码和一些回调URL进行轮询以获取结果.

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.

所以问题是: 1.如何使JAX-RS返回202代码? 2.如何将回调URL传递给客户端?

So the question is: 1. How to make JAX-RS return 202 code? 2. How to pass callback url to the client?

推荐答案

让postSomething方法返回Response对象:

Have the postSomething method return a Response object:

public Response postSomething(@PathParam("name") String name, MyObject data) {
   return Response.status(Status.ACCEPTED).build();
}

如果您希望HTTP正文中的回调URI为纯文本格式,则可以执行以下操作:

If you want the callback URI as plain-text in the HTTP Body you could do something like this:

public Response postSomething(@PathParam("name") String name, MyObject data) {
   return Response.status(Status.ACCEPTED).entity("http://google.com").build();
}

要通过资源类生成URI,请查看 UriBuilder

For generating URIs by resource classes, have a look at UriBuilder

这篇关于用JAX-RS异步REST调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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