简体   繁体   English

报告基于Jersey的RESTful服务的进度

[英]Reporting progress from RESTful service based on Jersey

I have a RESTful service developed using Jersey(Java) which does heavy lifting for a user including the multiple file transfer part of a single complete operation. 我有一个使用Jersey(Java)开发的RESTful服务,该服务为用户带来了繁重的工作,包括单个完整操作的多个文件传输部分。 I would like to provide some sort of progress notification to the client(user) using this service indicating what is going or what percent of the operation is done. 我想使用此服务向客户端(用户)提供某种进度通知,指示正在进行的操作或完成的操作百分比。 Accuracy of the notification is not an issue. 通知的准确性不是问题。 Given that this is a RESTful service, any thought how this could be achieved? 鉴于这是一个RESTful服务,是否想到如何实现? Any existing sample might be helpful. 任何现有的样本可能会有所帮助。

Update/clarification: This notification is like progress/status notification while the main operation is still in progress, since in certain scenarios the whole operation might take more than 5 minutes I want to make sure I update the client as to what is going on on the service side and why it is taking this long to fulfill the request. 更新/澄清:此通知就像进度/状态通知,而主要操作仍在进行中,因为在某些情况下,整个操作可能需要5分钟以上的时间,所以我想确保我更新客户端有关正在进行的操作服务端,以及花这么长时间才能满足要求的原因。

Ex. 防爆。 Request ... status... status .... status.... final Response. 请求...状态...状态...状态...最终响应。

I am assuming that this is not going to happen on same Request/Response pipe, this might involve creating another communication pipe between client and service. 我假设这不会在相同的请求/响应管道上发生,这可能涉及在客户端和服务之间创建另一个通信管道。

When I asked a similar question I was suggested to respond with HTTP 202 Accepted and provide an address of a newly created resource - a status of the operation: 当我问类似的问题时 ,建议我以HTTP 202 Accepted响应并提供新创建资源的地址-操作状态:

HTTP 202 Accepted
Location: /jobs/statuses/<status_id>

You can return a json string with all the information. 您可以返回带有所有信息的json字符串。 The way you generate the information for monitoring is up to you. 生成监视信息的方式取决于您。

Here is an example of controller with json: 这是带有json的控制器的示例:

@RequestMapping(value = "/getblabla", method = RequestMethod.POST)
public @ResponseBody String getblabla(@RequestParam("json") String json,
    HttpServletRequest request) {

    // Do something

    return resJson;
}

Then you can use a library like Gson that has two main methods. 然后,您可以使用像Gson这样的具有两种主要方法的库。

Here's how to map a json to an object: 这是将json映射到对象的方法:

 MyDto obj = gson.fromJson(json, MyDto.class);

Here's how to map an object to a Json: 将对象映射到Json的方法如下:

String json = gson.toJson(obj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM