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
15
votes
7 answers

ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

I am completely a beginner at Spring (you can see that in my code :) ). I just wanted to test the class RestTemplate but I got a ClassNotFoundException. So the code is: import org.springframework.context.ApplicationContext; import…
Viktor
  • 1,325
  • 2
  • 19
  • 41
15
votes
2 answers

How to send a getForObject request with parameters Spring MVC

I have a method on the Server side which gives me information about an specific name registered in my database. I'm accessing it from my Android application. The request to Server is done normally. What I'm trying to do is to pass parameter to the…
Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61
15
votes
3 answers

Force Spring RestTemplate to use XmlConverter

We are integrating with a third party that is sending xml with the content-type header as text/html. We were planning on using Spring's RestTemplate to map it to classes we've generated from xsds, but the RestTemplate fails to find an appropriate…
Nathanial
  • 2,177
  • 1
  • 15
  • 34
14
votes
5 answers

SpringBoot upgrade RestTemplateBuilder from 1.5.14 to 2.1.5

I have this piece of code working fine on a project that uses RestTemplateBuilder 1.5.14 this.restTemplate = restTemplateBuilder .setConnectTimeout(connectTimeout) .setReadTimeout(readTimeout) …
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
14
votes
2 answers

Retrieve a cookie using Spring RestTemplate

I need to get a cookie from a server using Spring RestTemplate. Do you guys know how I can perform this? Thank you for your help!
user8147290
14
votes
1 answer

Spring RestTemplate Connection Timeout is not working

I am trying to configure time out when external web service call. I am calling external web service by Spring Rest Template in my service. For connection timeout testing purpose, the external web service is stopped and application server is down. I…
14
votes
3 answers

REST POST works correctly with POSTMAN but exception when using Spring RestTemplate

I am writing a Rest client to post JSON data using Spring RestTemplate. Using POSTMAN and following JSON data in body get the response correctly- { "InCode":"test", "Name":"This is test", "Email":"test@gmail.com", …
Rehan
  • 929
  • 1
  • 12
  • 29
14
votes
2 answers

How to disable encoding using RestTemplate

I am using REST template to intentionally send a % in request uri, something like /items/a%b String responseEntity = restTemplate.exchange("/items/a%b", requestObj.getHttpMethod(), requestEntity, String.class); The restTemplate is…
tintin
  • 5,676
  • 15
  • 68
  • 97
14
votes
1 answer

How to follow Single Responsibility principle in my HttpClient executor?

I am using RestTemplate as my HttpClient to execute URL and the server will return back a json string as the response. Customer will call this library by passing DataKey object which has userId in it. Using the given userId, I will find out what…
john
  • 11,311
  • 40
  • 131
  • 251
14
votes
1 answer

Why RestTemplate GET response is in JSON when should be in XML?

I struggled with an extrange spring behavior using RestTemplate (org.springframework.web.client.RestTemplate) without success. I use in my hole application below code and always receive an XML response, which I parse and evaluate its result. String…
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
14
votes
6 answers

How to generate Java client proxy for RESTful service implemented with Spring?

We use Spring to implement REST controller, for example: @Controller @RequestMapping("/myservice") public class MyController { @RequestMapping(value = "foo", method = RequestMethod.GET) public @ResponseBody string foo() {...} } I can call…
Dima
  • 699
  • 1
  • 6
  • 18
14
votes
3 answers

How to Pass custom objects using Spring's REST Template

I have a requirement to pass a custom object using RESTTemplate to my REST service. RestTemplate restTemplate = new RestTemplate(); MultiValueMap requestMap = new LinkedMultiValueMap(); ... requestMap.add("file1",…
ASChakkalakal
  • 459
  • 2
  • 8
  • 18
13
votes
2 answers

How to post byte array via RestTemplate

Goal: Post Image using RestTemplate Currently using a variation of this MultiValueMap parts = new LinkedMultiValueMap(); parts.add("field 1", "value 1"); parts.add("file",…
lemon
  • 9,155
  • 7
  • 39
  • 47
13
votes
3 answers

no String-argument constructor/factory method to deserialize from String value - Exception while deserializing json object from restTemplate

Facing issue while making call to retrieve a json response and parse it. [ { "name": "john doe", "age": "24", "address": "{\"state\":\"LA\",\"country\":\"US\"}" …
Sriram G
  • 161
  • 1
  • 1
  • 10
13
votes
1 answer

Correct usage of ParameterizedTypeReference

In a test I wish to hit an endpoint which returns a list of a type. Currently I have @Test public void when_endpoint_is_hit_then_return_list(){ //Given ParameterizedTypeReference> responseType = new…
shirafuno
  • 387
  • 3
  • 9
  • 24