How to fixed response time to call REST API in java?

rest api response best practices
rest api response format best practices
rest api wait for response
how to handle multiple request in rest api
rest api example
wait for api response java
rest assured wait for response
rest api design example

I want to call REST API by java programming. And I also want to give a time limitation during calling that API. If response time take more than 10 second than I want to disconnect API calling and print a message that response time is more than 10 second.

Please help me by given example code of java.

Given bellow the source code of calling API.

JSONParserPost jsonParserpost = new JSONParserPost();
        String output = jsonParserpost.makeHttpRequest(URL, "POST", request); 
        System.out.println("Row output :"+ output.toString());
        JSONObject jsonObject = new JSONObject(output);
        if(jsonObject != null) 
            responeXML = (String)jsonObject.get("response");

Here in 2nd line I've called a REST API. Now I want to fixed a time limit on duration of response of REST API.


URL url = new URL(this.serviceURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("Accept", "application/xml;");


 * connection timeout value to 20 seconds
int apiReadTimeOut = 20; // 20 seconds
connection.setConnectTimeout(apiReadTimeOut * 1000);
connection.setReadTimeout(apiReadTimeOut * 1000);


HttpClient httpClient = null;

* connection timeout value to 20 seconds
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
httpClient = new DefaultHttpClient(httpParams);

How to wait for an api request to return a response?, You need to wait until client receives response or request times out. REST- Assured uses Apache HTTP Client for which you can set TimeLimitedCodeBlock is described in answers to Java: set timeout on a certain The separate thread terminates when HTTP Response is received or time out passes. Today I wanted to go over a new feature — measure response time — that was just introduced in the REST Assured 2.8.0 version released on December 12. This might not seem like a big change, but for my team, it is, because we sometimes have requirements that need to meet a certain time threshold.

You can use spring restTemplate

    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        ((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setConnectTimeout(milisecond);
        ((SimpleClientHttpRequestFactory) restTemplate.getRequestFactory()).setReadTimeout(milisecond);

        return restTemplate;

Please find example -

Best practices for REST API design, REST APIs should accept JSON for request payload and also send responses Set the Content-Type header in the response to application/json; We also need ways to paginate data so that we only return a few results at a time. verbs in your API endpoints then simply removing the verbs won't fix the� Note: TPM REST API responds with the according file content reference. This following example is to retrieve the content value of the FileScriptFile property of a File transport named file under the EZComm protocol for partner Partner1:

If you are using httpClient this following link can help you from my understanding of your problem. Apache HttpClient Timeout.

int CONNECTION_TIMEOUT_MS = timeoutSeconds * 1000; // Timeout in millis.
RequestConfig requestConfig = RequestConfig.custom()

HttpPost httpPost = new HttpPost(URL);

How a RESTful API server reacts to requests – O'Reilly, Learn how to properly design RESTful APIs communication with clients, Depending on the error the API returned, what clients can do to fix the request? The status code is part of the HTTP response, as you can see in the first line of this If a client misses that time and tries to change the rent, a 400 Bad� However, this only makes sense when your "second REST API" call is actually implemented in another method in your application and lives within the same JRE. Answer 3 I'd say yes and no; there is nothing "wrong" about calling another service asynchronously (ex: a metrics subsystem to register that a call to that service was made).

Lifecycle of a Request-Response Process for a Spring REST API , Lifecycle of a Request-Response Process for a Spring REST API When an incoming request matches the requirements specified by the the job of marshaling the Java object to the response type requested by the client. The RESTful services from last “Jackson + JAX-RS” article will be reused, and we will use “” and “” to create a simple Java client to send “GET” and “POST” request.

How to Deal With Slow APIs, As an engineer, you spend a lot of time with APIs -- you're either Let's assume that your API call to fetch a 1-year trend chart got worse in this case, is to not deal with the response coming out of a slow API but The rest are all taken care of by the RxJS framework. Should I Parallelize Java 8 Streams? In this guide you will learn how to create an API using ktor. We are going to create a simple API to store simple text snippets (like a small pastebin-like API). To achieve this, we are going to use the Routing, StatusPages, Authentication, JWT Authentication, CORS, ContentNegotiation and Jackson features.

Creating a REST API, Then other applications would call your REST API, which allows you to logic in one place instead of rewriting it every time you wanted to create a new application. This code uses the Java library to escape the String values before The response is the JSON our REST API outputs, which this client program� Jira REST APIs provide access to resources (that is, data entities) via URI paths. To use a REST API, your application makes an HTTP request and parse the response. The Jira REST API uses JSON as its communication format and the standard HTTP methods like GET, PUT, POST, and DELETE. URIs for Jira REST API resource have the following structure:

  • Please share your implementation here for better suggestion.
  • Dear Arun Kumar, thanks for your response. I have attached my source code. Please see this and response me again.
  • you can try this or
  • Please see the link -…