mohammad
mohammad

Reputation: 57

REST service doesn't respond after 1 min from request

We have RESTful API in our backend and there is this important service that needs more than 1 min to get ready to response.

So after about 90 seconds the response is ready and the process is finished but the browser doesn't get any response from the server (pending) and then finally it fails (image is below). I have tested the server with low data and approved that it only happens when response takes more than 1 minute to get ready. How can I fix this issue?

Response failed after nothing happens

This is the service :

@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();
}

NOTE: we are using apache-tomcat 9.0.8 , JAVA 8! imported dependencies:

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'

Upvotes: 1

Views: 1705

Answers (1)

MvdD
MvdD

Reputation: 23494

Instead of returning a HTTP 200 OK response when all the results are in, you can return a HTTP 202 Accepted response immediately with a Location header where the client can retrieve the results once they're in.

The client would then poll the URL in the Location header until all results are ready using the GET method.

If the client retrieves the results before they are ready, return a HTTP 404 Not Found response, optionally with a 'Retry-After` header.

When the client retrieves the completed results, just return a HTTP 200 OK response with all the results in the body.

Upvotes: 2

Related Questions