简体   繁体   English

如何发送 Stream<string> 作为来自 REST API 的客户的响应?</string>

[英]How to send Stream<String> as response to client from REST API?

I have a restful call to the server from where I want to return String as Stream (Stream the data).我对服务器有一个安静的调用,我想从该服务器返回字符串为 Stream(流式传输数据)。 In the current code implementation, I'm storing the data as List<String> and sending the ResponseEntity containing that list as a response.在当前的代码实现中,我将数据存储为 List<String> 并将包含该列表的 ResponseEntity 作为响应发送。 But the List contains huge data and we want to achieve memory optimization.但是 List 包含大量数据,我们希望实现 memory 优化。 So, as a solution we want to stream that List<String> chunk by chunk from the rest api and would do something with the data on the client side.因此,作为一种解决方案,我们希望 stream 从 rest api 中逐块列出 <String> ,并对客户端的数据进行处理。 I don't want to write the data directly to OutputStream which the StreamingResponseBody allows.我不想将数据直接写入 StreamingResponseBody 允许的 OutputStream 。

Current Api:当前 Api:

@RestController
@RequestMapping(value = "/rest/api/")
public class StringRestController {

    @RequestMapping(value = "/stream", method = RequestMethod.GET)
    public ResponseEntity<List<String>> getMessage(HttpServletRequest request, HttpServletResponse response) {
        
        List<String> mList = new ArrayList<>();
        
        List<Long> dataList = getListFromDb();

        //Here do something with dataList and store List<String> in mList

        if (CollectionUtils.isEmpty(mList)) {
            return ResponseEntity.notFound().build();
        } else {
            return new ResponseEntity<>(mList, HttpStatus.OK);
        }

    }
}

I want the data to be streamed as the List<String> is huge.我希望数据被流式传输,因为 List<String> 很大。 Sending all at once has performance issues as well as from the memory optimization point of view it can cause issues.一次性发送存在性能问题,并且从 memory 优化的角度来看,它可能会导致问题。 We're using Java 1.8 and Spring 3.0 for the same.我们同样使用 Java 1.8 和 Spring 3.0。

I would suggest you implement the chunking yourself.我建议你自己实现分块。

The chunking you describe only deals with the transport of the data to the client.您描述的分块仅涉及将数据传输到客户端。 You already mention that "from the memory optimization point of view it can cause issues".您已经提到“从 memory 优化的角度来看它可能会导致问题”。 The big issue here is that yor load the whole List from the database.这里的大问题是你从数据库中加载整个列表。 That needs to be chunked too.这也需要分块。

So implement that only a certain number of elements (like 100) are loaded from the database by default.因此,实现默认情况下仅从数据库加载一定数量的元素(如 100 个)。 Tell the client in the response "here are 100, but there are more".在响应中告诉客户“这里有 100 个,但还有更多”。 Then the client needs to send additional requests with the info "i already have 100, give me 100 more".然后客户需要发送附加请求,其中包含“我已经有 100 个,再给我 100 个”的信息。

So as you can see that is nothing that can be done with a simple annotation or something, it requires that you change your api fields.因此,正如您所看到的,使用简单的注释或其他东西无法做到这一点,它需要您更改 api 字段。

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

相关问题 Streaming Rest Api - 从 api 以 stream 的形式向客户端发送数据块 - Streaming Rest Api - Send data in chunks to the client in a stream from the restful api 如何从其余Web服务发送响应 - How to send response from rest webservice 如何从Rest API登录的泽西岛客户端响应中获取Cookie到期日期? - How to get cookie expiry date from jersey client response from rest api login? 如何将响应从servlet发送到客户端? - How to send response from servlet to client side? 如何在后端触发作业时立即从 rest api 发送响应? - how to send response immediately from rest api when it triggers a job in the backend? 如何将数据从服务器(REST api 响应)推送/发送到 jsp/html 表单 - How to push/send data from server (REST api response) to a jsp/html form 如何将数据从servlet发送到rest api - how to send data from servlet to rest api 如何从Spring Rest Controller发送流中的文本块? - How to send text chunks in stream from spring rest controller? 如何从Java发送Bluemix WAV流响应 - How to send bluemix wav stream response from java 试图从CAS安全的REST API获取响应,但是从Java客户端获取登录页面作为响应 - trying to get response from CAS secure rest api,but getting login page as response from java client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM