Sending Multipart File as POST parameters with RestTemplate requests

A way to solve this without needing to use a FileSystemResource that requires a file on disk, is to use a ByteArrayResource, that way you can send a byte array in your post (this code works with Spring 3.2.3): MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); final String filename=”somefile.txt”; map.add(“name”, filename); map.add(“filename”, filename); ByteArrayResource contentsAsResource … Read more

How to set an “Accept:” header on Spring RestTemplate request?

I suggest using one of the exchange methods that accepts an HttpEntity for which you can also set the HttpHeaders. (You can also specify the HTTP method you want to use.) For example, RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); HttpEntity<String> entity = new HttpEntity<>(“body”, headers); restTemplate.exchange(url, HttpMethod.POST, entity, String.class); I … Read more

Spring RestTemplate timeout

For Spring Boot >= 1.4 @Configuration public class AppConfig { @Bean public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(…) .setReadTimeout(…) .build(); } } For Spring Boot <= 1.3 @Configuration public class AppConfig { @Bean @ConfigurationProperties(prefix = “custom.rest.connection”) public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() { return new HttpComponentsClientHttpRequestFactory(); } @Bean public RestTemplate customRestTemplate() { return new RestTemplate(customHttpRequestFactory()); } } … Read more

Multipart File Upload Using Spring Rest Template + Spring Web MVC

The Multipart File Upload worked after following code modification to Upload using RestTemplate LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add(“file”, new ClassPathResource(file)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>( map, headers); ResponseEntity<String> result = template.get().exchange( contextPath.get() + path, HttpMethod.POST, requestEntity, String.class); And adding MultipartFilter to web.xml <filter> <filter-name>multipartFilter</filter-name> <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> … Read more

Get list of JSON objects with Spring RestTemplate

First define an object to hold the entity coming back in the array.. e.g. @JsonIgnoreProperties(ignoreUnknown = true) public class Rate { private String name; private String code; private Double rate; // add getters and setters } Then you can consume the service and get a strongly typed list via: ResponseEntity<List<Rate>> rateResponse = restTemplate.exchange(“https://bitpay.com/api/rates”, HttpMethod.GET, null, … Read more

Spring Resttemplate exception handling

You want to create a class that implements ResponseErrorHandler and then use an instance of it to set the error handling of your rest template: public class MyErrorHandler implements ResponseErrorHandler { @Override public void handleError(ClientHttpResponse response) throws IOException { // your error handling here } @Override public boolean hasError(ClientHttpResponse response) throws IOException { … } … Read more

Spring RestTemplate – how to enable full debugging/logging of requests/responses?

Just to complete the example with a full implementation of ClientHttpRequestInterceptor to trace request and response: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor { final static Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class); @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) … Read more