简体   繁体   English

如何在Spring Boot应用程序中处理来自外部api的较大响应

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

In our spring boot application, we are calling the external API for some purpose, it is returning around 20mb JSON data as the response. 在我们的Spring Boot应用程序中,出于某种目的,我们正在调用外部API,它会返回大约20mb的 JSON数据作为响应。 After receiving the response, using ObjectMapper we are mapping the response to a POJO. 收到响应后,使用ObjectMapper将响应映射到POJO。

We are using RestTemplate to call the API and receive the response. 我们正在使用RestTemplate来调用API并接收响应。

What are the best practices to handle large response data without getting memory issues in Spring boot application? 在Spring Boot应用程序中处理大型响应数据而又不会出现内存问题的最佳实践是什么?

Thanks 谢谢

I would say there are two good approaches: 我会说有两种好的方法:

  • Avoid having APIs that return so much information. 避免使用返回大量信息的API。 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". 我个人认为,REST端点应遵循与我们的代码相同的规则,例如“单一责任原则”或“关注点分离”。 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. 示例:拥有两个API而不是使用返回“完整目录”(包含所有目录条目的所有详细信息)的API,而是使用两个API:一个仅检索目录中所有条目的ID /名称,另一个返回所有详细信息一个(或多个)条目。
  • 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. 或者,如用户Antoniosssss的评论中所建议:查看是否可以避免使用“完整”响应,而是使用某种基于流的解决方案。

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. 可能会考虑不要将对象映射用于完整的响应。

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

相关问题 如何在 Spring Boot 中使用 RestTemplate 从来自外部 REST API 的 JSON 响应中提取数据? - How to extract data from JSON response coming from external REST API using RestTemplate in Spring Boot? 使用 Spring Boot 从外部源(例如 API)注入应用程序属性 - Inject application properties from an external source such as API with Spring Boot 如何处理响应<!--?-->输入 spring-boot? - How to handle response with <?> type in spring-boot? 推荐的方法来处理spring-boot应用程序/外部配置? - Recommended way to handle spring-boot application/external config? Java Spring Boot 如何知道客户端收到来自 API 的响应 - Java Spring Boot how to know client receive the response from API Spring Boot API响应(application / json)转换为响应(text / xml) - Spring boot API response(application/json) convert to response (text/xml) 如何在Spring Boot中测试对外部api的调用 - How to test a call to external api in Spring Boot 如何在 Spring 引导中使用外部 API - How to consume an external API in Spring Boot 如何启用 Spring Boot 连接到外部 API - How to enable Spring Boot to connect to external API 一旦在 Spring Boot 中收到两个响应,Call 2 External API 如何并行并执行合并 - How Call 2 External API parallel and perform merge once both response are received in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM