How to autowire RestTemplate using annotations

Errors you’ll see if a RestTemplate isn’t defined Consider defining a bean of type ‘org.springframework.web.client.RestTemplate’ in your configuration. or No qualifying bean of type [org.springframework.web.client.RestTemplate] found How to define a RestTemplate via annotations Depending on which technologies you’re using and what versions will influence how you define a RestTemplate in your @Configuration class. Spring >= … Read more

Access Https Rest Service using Spring RestTemplate

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File(keyStoreFile)), keyStorePassword.toCharArray()); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadKeyMaterial(keyStore, keyStorePassword.toCharArray()) .build(), NoopHostnameVerifier.INSTANCE); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory( socketFactory).build(); ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory); MyRecord record = restTemplate.getForObject(uri, MyRecord.class); LOG.debug(record.toString());

Disabling SSL Certificate Validation in Spring RestTemplate

@Bean public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() .loadTrustMaterial(null, acceptingTrustStrategy) .build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(csf) .build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory); return restTemplate; }

spring mvc rest service redirect / forward / proxy

You can mirror/proxy all requests with this: private String server = “localhost”; private int port = 8080; @RequestMapping(“/**”) @ResponseBody public String mirrorRest(@RequestBody String body, HttpMethod method, HttpServletRequest request) throws URISyntaxException { URI uri = new URI(“http”, null, server, port, request.getRequestURI(), request.getQueryString(), null); ResponseEntity<String> responseEntity = restTemplate.exchange(uri, method, new HttpEntity<String>(body), String.class); return responseEntity.getBody(); } This will … Read more

How to POST form data with Spring RestTemplate?

The POST method should be sent along the HTTP request object. And the request may contain either of HTTP header or HTTP body or both. Hence let’s create an HTTP entity and send the headers and parameter in body. HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>(); map.add(“email”, “first.last@example.com”); HttpEntity<MultiValueMap<String, String>> … Read more

Download Large file from server using REST template Java Spring MVC

Here is how I do it. Based on hints from this Spring Jira issue. RestTemplate restTemplate // = …; // Optional Accept header RequestCallback requestCallback = request -> request.getHeaders() .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL)); // Streams the response instead of loading it all in memory ResponseExtractor<Void> responseExtractor = response -> { // Here I write the response to … Read more

Using Spring RestTemplate in generic method with generic parameter

No, it is not a bug. It is a result of how the ParameterizedTypeReference hack works. If you look at its implementation, it uses Class#getGenericSuperclass() which states Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class. If the superclass is a parameterized type, the … Read more

Spring RestTemplate with paginated API

When migrating from Spring Boot 1.x to 2.0, changed the code reading the Rest API response as import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.JsonNode; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import java.util.ArrayList; import java.util.List; public class RestPageImpl<T> extends PageImpl<T>{ @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) public RestPageImpl(@JsonProperty(“content”) List<T> content, @JsonProperty(“number”) int number, @JsonProperty(“size”) int size, @JsonProperty(“totalElements”) Long totalElements, @JsonProperty(“pageable”) … Read more