Retry java RestTemplate HTTP request if host offline

I had same situation and done some googling found the solution. Giving answer in hope it help someone else. You can set max try and time interval for each try. @Bean public RetryTemplate retryTemplate() { int maxAttempt = Integer.parseInt(env.getProperty(“maxAttempt”)); int retryTimeInterval = Integer.parseInt(env.getProperty(“retryTimeInterval”)); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(maxAttempt); FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); backOffPolicy.setBackOffPeriod(retryTimeInterval); … Read more

Why do I always get 403 when fetching data with RestTemplate? [duplicate]

Try to add a “User-Agent” header to your request. Change your code to: try { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.add(“user-agent”, “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36”); HttpEntity<String> entity = new HttpEntity<String>(“parameters”, headers); Object response = restTemplate.exchange(“https://api.hearthstonejson.com/v1/19776/enUS/cards.json”, HttpMethod.GET,entity,Object.class); System.out.println(response); } catch (Exception ex) … Read more

RestTemplate client with cookies

RestTemplate has a method in which you can define Interface ResponseExtractor<T>, this interface is used to obtain the headers of the response, once you have them you could send it back using HttpEntity and added again. .add(“Cookie”, “SERVERID=c52”); Try something like this. String cookieHeader = null; new ResponseExtractor<T>(){ T extractData(ClientHttpResponse response) { response.getHeaders(); } } … Read more

Setting request header content-type to json in Spring Framework resttemplate [duplicate]

you can try using any method from below code 1 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers); restTemplate.put(uRL, entity); 2 RequestEntity<String> requestEntity = RequestEntity .post(new URL(attributeLookupUrl).toURI()) .contentType(MediaType.APPLICATION_JSON) .body(postBodyJson); restTemplate.exchange(requestEntity, responseClass); 3 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // if you need to pass form parameters in request with headers. MultiValueMap<String, … Read more

Could not read JSON: Can not deserialize instance of hello.Country[] out of START_OBJECT token

You need to do the following: public class CountryInfoResponse { @JsonProperty(“geonames”) private List<Country> countries; //getter – setter } RestTemplate restTemplate = new RestTemplate(); List<Country> countries = restTemplate.getForObject(“http://api.geonames.org/countryInfoJSON?username=volodiaL”,CountryInfoResponse.class).getCountries(); It would be great if you could use some kind of annotation to allow you to skip levels, but it’s not yet possible (see this and this)

How to parse the response body in Java, when the HTTP request has return status 401

Try the following approach without needing a custom handler. The idea is to get the response as a string from the HttpStatusCodeException, and then you can convert it to your object. For the conversion I used the Jackson’s ObjectMapper: try { restTemplate.postForObject(url, pojoInstance, responseClass); } catch (HttpStatusCodeException e) { if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) { String … Read more

Springs RestTemplate default connection pool

Yes, Spring RestTemplateBuilder uses Apache HttpClient for pooling (usage). RestTemplateBuilder creates HttpComponentsClientHttpRequestFactory and uses HttpClientBuilder. HttpClientBuilder, by default, sets pool size per route (host) to 5 and total pool size to 10 (source): s = System.getProperty(“http.maxConnections”, “5”); int max = Integer.parseInt(s); poolingmgr.setDefaultMaxPerRoute(max); poolingmgr.setMaxTotal(2 * max); To check connection pool logging set logging level as follows: … Read more

What is the restTemplate.exchange() method for?

The method documentation is pretty straightforward: Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity. URI Template variables are expanded using the given URI variables, if any. Consider the following code extracted from your own question: ResponseEntity<byte[]> result = restTemplate.exchange(“http://localhost:7070/spring-rest-provider/krams/person/{id}”, HttpMethod.GET, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)