Senior
What configuration parameters do we pass to the HTTP client (RestTemplate/WebClient/Feign) for calling an external service?
sobes.tech AI
Answer from AI
When calling an external service via HTTP clients (RestTemplate, WebClient, Feign), the following parameters are usually configured:
- URL and request path — the address of the called service.
- HTTP method — GET, POST, PUT, DELETE, etc.
- Headers — for example,
Authorizationfor tokens,Content-Type,Accept. - Request body — for POST/PUT requests, serialized into JSON, XML, etc.
- Query parameters — for GET requests.
- Timeouts — connection and response wait times to prevent hanging.
- Error handling and retries — policy in case of failures.
- Encoding and serialization/deserialization — data format settings.
Example of configuring RestTemplate with timeouts and headers:
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer token");
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {{
setConnectTimeout(5000);
setReadTimeout(5000);
}});
ResponseEntity<String> response = restTemplate.exchange(
"https://api.example.com/resource",
HttpMethod.POST,
entity,
String.class
);
For WebClient and Feign, timeouts, headers, and serialization are configured similarly.