How to handle large response from external api in Spring Boot application

Viewed 1248

In our spring boot application, we are calling the external API for some purpose, it is returning around 20mb JSON data as the response. After receiving the response, using ObjectMapper we are mapping the response to a POJO.

We are using RestTemplate to call the API and receive the response.

What are the best practices to handle large response data without getting memory issues in Spring boot application?

Thanks

1 Answers

I would say there are two good approaches:

  • Avoid having APIs that return so much information. Personally, I think that REST endpoints should follow the same rules that we use for our code, such as "single responsibility principle" or "separation of concerns". Example: instead of having an API that returns a "full catalog" (with all the details for all catalog entries), have two APIs: one to retrieve just the ids/names of all entries in the catalog, and one that returns all details of one (or more) entries.
  • Or, as suggested in the comment by user Antoniossss: see if you can avoid using "full" responses, but instead use some sort of streaming-based solution.

But having said that: when your requirement is really to return all that data with one call, then there isn't too much you can do. You might consider to not use object mapping for the complete response then.

Related