Follow 302 redirect using Spring restTemplate?

Using the default ClientHttpRequestFactory implementation – which is the SimpleClientHttpRequestFactory – the default behaviour is to follow the URL of the location header (for responses with status codes 3xx) – but only if the initial request was a GETrequest. Details can be found in this class – searching for the following method: protected void prepareConnection(HttpURLConnection … Read more

What is the right way to send a client certificate with every request made by the resttemplate in spring?

Here is example how to do this using RestTemplate and Apache HttpClient You should define your own RestTemplate with configured SSL context: @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception { char[] password = “password”.toCharArray(); SSLContext sslContext = SSLContextBuilder.create() .loadKeyMaterial(keyStore(“classpath:cert.jks”, password), password) .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(); HttpClient client = HttpClients.custom().setSSLContext(sslContext).build(); return builder .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) .build(); } private … Read more

RestTemplate – Handling response headers/body in Exceptions (RestClientException, HttpStatusCodeException)

I finally did it using ResponseErrorHandler. public class CustomResponseErrorHandler implements ResponseErrorHandler { private static ILogger logger = Logger.getLogger(CustomResponseErrorHandler.class); private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler(); public void handleError(ClientHttpResponse response) throws IOException { List<String> customHeader = response.getHeaders().get(“x-app-err-id”); String svcErrorMessageID = “”; if (customHeader != null) { svcErrorMessageID = customHeader.get(0); } try { errorHandler.handleError(response); } catch (RestClientException scx) … Read more

Spring RestTemplate invoking webservice with errors and analyze status code

You need to implement ResponseErrorHandler in order to intercept response code, body, and header when you get non-2xx response codes from the service using rest template. Copy all the information you need, attach it to your custom exception and throw it so that you can catch it in your test. public class CustomResponseErrorHandler implements ResponseErrorHandler … Read more

How to set multiple headers at once in Spring WebClient?

If those headers change on a per request basis, you can use: webClient.get().uri(“/resource”).headers(httpHeaders -> { httpHeaders.setX(“”); httpHeaders.setY(“”); }); This doesn’t save much typing; so for the headers that don’t change from one request to another, you can set those as default headers while building the client: WebClient webClient = WebClient.builder().defaultHeader(“…”, “…”).build(); WebClient webClient = WebClient.builder().defaultHeaders(httpHeaders … Read more

resttemplate getForObject map responsetype

RestTemplate has a method named exchange that takes an instance of ParameterizedTypeReference as parameter. To make a GET request that returns a java.util.Map, just create an instance of an anonym class that inherits from ParameterizedTypeReference. ParameterizedTypeReference<Map<String, String>> responseType = new ParameterizedTypeReference<>() {}; You can then invoke the exchange method: RequestEntity<Void> request = RequestEntity.get(“http://example.com/foo”) .accept(MediaType.APPLICATION_JSON).build(); Map<String, … Read more

RestTemplate: exchange() vs postForEntity() vs execute()

The RestTemplate is a very versatile object. Let’s start with execute, since it’s the most generic method: execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Object… uriVariables) Note the uriVariables can be passed as a Map too. execute is designed to be applicable in the highest variety of scenarios possible: The first and … Read more

tech