首页 > 解决方案 > 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'

标签: javaresttomcatservice

解决方案


您可以立即返回带有标题的响应,而不是HTTP 200 OK在所有结果都在时返回响应,客户端可以在结果进入后检索结果。HTTP 202 AcceptedLocation

然后,客户端将轮询标头中的 URL,直到使用该方法Location准备好所有结果。GET

如果客户端在结果准备好之前检索结果,则返回一个HTTP 404 Not Found响应,可选择带有“Retry-After”标头。

当客户端检索到完成的结果时,只需返回一个HTTP 200 OK包含正文中所有结果的响应。


推荐阅读