Questions tagged [resttemplate]

Use this tag for Spring's RestTemplate, a class for client-side HTTP communications and RESTful principles enforcement.

The RestTemplate is the central Spring class for client-side HTTP access. Conceptually, it is very similar to the JdbcTemplate, JmsTemplate, and the various other templates found in the Spring Framework and other portfolio projects.

This means, for instance, that the RestTemplate is thread-safe once constructed, and that you can use callbacks to customize its operations.

RestTemplate Methods

-------------------------------------------------------------
| HTTP          | RESTTEMPLATE                               |
-------------------------------------------------------------
| DELETE        | delete(String, String...)                  |
-------------------------------------------------------------
| GET           | getForObject(String, Class, String...)     | 
-------------------------------------------------------------
| HEAD          | headForHeaders(String, String...)          | 
-------------------------------------------------------------
| OPTIONS       | optionsForAllow(String, String...)         | 
-------------------------------------------------------------
| POST          | postForLocation(String, Object, String...) | 
-------------------------------------------------------------
| PUT           | put(String, Object, String...)             | 
-------------------------------------------------------------

Reference: https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate

Error Handling and Customization of Errors

Implement and inject the ResponseErrorHandler interface in a RestTemplate instance – to gracefully handle HTTP errors returned by remote APIs.

By default, the RestTemplate will throw one of these exceptions in case of an HTTP error:

  • HttpClientErrorException – in case of HTTP status 4xx
  • HttpServerErrorException – in case of HTTP status 5xx
  • UnknownHttpStatusCodeException – in case of an unknown HTTP status

One Can configure a Spring RestTemplate bean as well

  • using the default RestTemplateBuilder
  • using a RestTemplateCustomizer
  • creating RestTemplateBuilder
2832 questions
53
votes
1 answer

RestTemplate vs Apache Http Client for production code in spring project

we have a Spring project that is about to go into production. Currently, the project is using Apache Http Client. There is a thought of using RestTemplate as HttpClient. I am digging around to see any notable advantage of using RestTemplate over…
brain storm
  • 30,124
  • 69
  • 225
  • 393
50
votes
1 answer

How to handle empty response in Spring RestTemplate

My Authorization service returs a http 204 on success and a http 401 on failure but no responseBody. I'm not able to consume this with the RestTemplate client. It fails attempting to serialize the response. The error from Jackson suggest that i turn…
Monish Sen
  • 1,773
  • 3
  • 20
  • 32
50
votes
2 answers

ClassCastException: RestTemplate returning List instead of List

I'm trying to access getter methods on my MyModelClass but my code is returning List instead of List. This is my code. List myModelClass = (List)…
Erick
  • 715
  • 1
  • 8
  • 15
48
votes
7 answers

Spring MVC - RestTemplate launch exception when http 404 happens

I have a rest service which send an 404 error when the resources is not found. Here the source of my controller and the exception which send Http 404. @Controller @RequestMapping("/site") public class SiteController { @Autowired private…
loic
  • 511
  • 1
  • 4
  • 5
48
votes
4 answers

How do I retrieve HTTP status code and response body when an RestClientException is thrown?

The methods of RestTemplate such as postForEntity() throw RestClientException. I would like to extract the HTTP status code and response body from that exception object in the catch block. How can I do that?
Vathanak
  • 481
  • 1
  • 4
  • 4
47
votes
4 answers

How do I read the response header from RestTemplate?

I am posting information to a web service using RestTemplate.postForObject. Besides the result string I need the information in the response header. Is there any way to get this? RestTemplate template = new RestTemplate(); String result =…
Eric Milas
  • 1,807
  • 2
  • 18
  • 29
46
votes
10 answers

Sending Multipart File as POST parameters with RestTemplate requests

I am working with Spring 3 and RestTemplate. I have basically, two applications and one of them have to post values to the other app. through rest template. When the values to post are Strings, it's work perfect, but when i have to post mixed and…
Mauro Monti
  • 1,068
  • 2
  • 12
  • 21
45
votes
8 answers

Spring RestTemplate with paginated API

Our REST APIs are returning results in Pages. Here is an example of one Controller @RequestMapping(value = "/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8") @ResponseStatus(HttpStatus.OK) public…
turgos
  • 1,464
  • 1
  • 17
  • 28
45
votes
3 answers

How to send Multipart form data with restTemplate Spring-mvc

I am trying to upload a file with RestTemplate to Raspberry Pi with Jetty. On Pi there is a servlet running: protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws…
Mateusz Mańka
  • 848
  • 1
  • 6
  • 18
44
votes
9 answers

Getting InputStream with RestTemplate

I am using URL class to read an InputStream from it. Is there any way I can use RestTemplate for this? InputStream input = new URL(url).openStream(); JsonReader reader = new JsonReader(new InputStreamReader(input,…
user1950349
  • 4,738
  • 19
  • 67
  • 119
43
votes
4 answers

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

I am working on Rest API using Spring boot and I had to access an application's endpoint. I used RestTemplate for it. I was able to do it using 2 methods, postForEntity(): responseEntity = restTemplate.postForEntity(uri, httpEntity,…
Praveen Kumar
  • 977
  • 3
  • 12
  • 26
43
votes
4 answers

How to forward large files with RestTemplate?

I have a web service call through which zip files can be uploaded. The files are then forwarded to another service for storage, unzipping, etc. For now the file is stored on the file system, then a FileSystemResource is built. Resource zipFile =…
Gabi
  • 687
  • 1
  • 7
  • 18
42
votes
4 answers

What is the restTemplate.exchange() method for?

Actually what does the restTemplate.exchange() method do? @RequestMapping(value = "/getphoto", method = RequestMethod.GET) public void getPhoto(@RequestParam("id") Long id, HttpServletResponse response) { logger.debug("Retrieve photo with id: "…
sneha
  • 553
  • 3
  • 6
  • 11
41
votes
6 answers

Springs RestTemplate default connection pool

Just wondering if RestTemplate out of the box uses connection pooling or does it simply establish a new connection each time ?
Sam
  • 861
  • 3
  • 10
  • 16
41
votes
2 answers

RestTemplate uriVariables not expanded

I try to access a rest endpoint by using springs RestTemplate.getForObject() but my uri variables are not expanded, and attached as parameters to the url. This is what I got so far: Map uriParams = new HashMap
user1145874
  • 959
  • 3
  • 13
  • 21