【问题标题】:REST service doesn't respond after 1 min from requestREST 服务在请求 1 分钟后没有响应
【发布时间】:2018-11-04 16:26:39
【问题描述】:

我们的后端有 RESTful API,而这项重要的服务需要 1 多分钟才能准备好响应。

所以大约 90 秒后响应准备就绪,进程完成,但浏览器没有从服务器获得任何响应(待定),然后最终失败(下图)。我已经用低数据测试了服务器,并批准它只有在响应需要超过 1 分钟才能准备好时才会发生。我该如何解决这个问题?

Response failed after nothing happens

这是服务:

@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'

【问题讨论】:

标签: java rest tomcat service


【解决方案1】:

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

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

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

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

【讨论】:

  • 你能给我一个例子,说明如何立即返回 202 并继续这个过程吗?我应该使用多线程吗?
  • 是的,您需要后台线程或其他进程来处理组合结果的任务。
猜你喜欢
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2018-07-30
  • 1970-01-01
  • 2020-08-19
  • 2015-10-28
相关资源
最近更新 更多