java - REST 服务在请求 1 分钟后没有响应
问题描述
我们的后端有 RESTful API,而这项重要的服务需要 1 分钟以上的时间才能准备好响应。
因此,大约 90 秒后,响应准备就绪,进程完成,但浏览器没有从服务器获得任何响应(待定),然后最终失败(下图)。我已经用低数据测试了服务器,并批准它只有在响应需要超过 1 分钟才能准备好时才会发生。我该如何解决这个问题?
这是服务:
@POST
@Path("/search")
public Response hotelSearch(@RequestBody InputValues value) {
/* sending request to several other API
retrieving data from PostgreSQL DB
creating a big DTO
*/
return Response.ok(DTO).build();
}
注意:我们使用的是 apache-tomcat 9.0.8 ,JAVA 8!导入的依赖项:
compile 'org.springframework:spring-web:4.3.6.RELEASE'
compile 'org.springframework:spring-orm:4.0.2.RELEASE'
compile 'org.springframework:spring-aspects:4.0.2.RELEASE'
compile 'org.springframework.security:spring-security-web:3.2.1.RELEASE'
compile 'org.springframework.security:spring-security-config:3.2.1.RELEASE'
compile 'org.springframework.security:spring-security-cas:3.2.1.RELEASE
compile 'org.glassfish.jersey.ext:jersey-spring3:2.6'
解决方案
您可以立即返回带有标题的响应,而不是HTTP 200 OK
在所有结果都在时返回响应,客户端可以在结果进入后检索结果。HTTP 202 Accepted
Location
然后,客户端将轮询标头中的 URL,直到使用该方法Location
准备好所有结果。GET
如果客户端在结果准备好之前检索结果,则返回一个HTTP 404 Not Found
响应,可选择带有“Retry-After”标头。
当客户端检索到完成的结果时,只需返回一个HTTP 200 OK
包含正文中所有结果的响应。
推荐阅读
- javascript - 在每个故事的故事书中应用不同的主题
- python-3.x - 如何使用 python-pptx 更新 X 轴上的日期范围
- python-3.x - 为什么 Syntaxerror f-string: Expecting '}' 在我的代码中发生?
- java - 在 Flutter 中使用时无法从 Android 本机 (Java) 获取当前视图
- python - 如何立即使用函数的返回?
- scilab - Scilab xcos:在 Simulation -> Set Context 中运行脚本或定义函数
- laravel - Laravel 中基于事件的触发控制台命令
- node.js - 拉进猫鼬不想工作。有什么建议么?
- c++ - 如何在 C++ 向量(特征)中获得最大的 n 值?
- python-3.x - 从包含子字符串的列表中删除元素,例如“第 1 页,共 67 页”或“第 2 页,共 67 页”