简体   繁体   中英

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). In the current code implementation, I'm storing the data as List<String> and sending the ResponseEntity containing that list as a response. But the List contains huge data and we want to achieve memory optimization. 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. I don't want to write the data directly to OutputStream which the StreamingResponseBody allows.

Current 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. Sending all at once has performance issues as well as from the memory optimization point of view it can cause issues. We're using Java 1.8 and Spring 3.0 for the same.

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". 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. Tell the client in the response "here are 100, but there are more". Then the client needs to send additional requests with the info "i already have 100, give me 100 more".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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