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
84
votes
12 answers

Using Spring RestTemplate in generic method with generic parameter

To use generic types with Spring RestTemplate we need to use ParameterizedTypeReference (Unable to get a generic ResponseEntity where T is a generic class "SomeClass") Suppose I have some class public class MyClass { int…
Artyom Kozhemiakin
  • 1,432
  • 1
  • 13
  • 13
82
votes
6 answers

How to autowire RestTemplate using annotations

When I try to autowire Spring RestTemplate, I am getting following error: nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.client.RestTemplate] found for…
Meeti Sharma
  • 1,072
  • 1
  • 7
  • 13
80
votes
9 answers

spring mvc rest service redirect / forward / proxy

I have build a web application using spring mvc framework to publish REST services. For example: @Controller @RequestMapping("/movie") public class MovieController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) public @ResponseBody…
nilgun
  • 10,460
  • 4
  • 46
  • 57
80
votes
4 answers

Making authenticated POST requests with Spring RestTemplate for Android

I have a RESTful API I'm trying to connect with via Android and RestTemplate. All requests to the API are authenticated with HTTP Authentication, through setting the headers of the HttpEntity and then using RestTemplate's exchange() method. All GET…
Nick Daugherty
  • 2,763
  • 4
  • 20
  • 17
79
votes
7 answers

RestTemplate: How to send URL and query parameters together

I am trying to pass path param and query params in a URL but I am getting a weird error. Below is the code. String url = "http://test.com/Services/rest/{id}/Identifier" Map params = new HashMap(); …
Shiva
  • 913
  • 2
  • 8
  • 7
78
votes
12 answers

How to disable SSL certificate checking with Spring RestTemplate?

I am trying to write an integration test where our test launches an embedded HTTPS server using Simple. I created a self-signed certificate using keytool and am able to access the server using a browser (specifically Chrome, and I do get a warning…
Sled
  • 18,541
  • 27
  • 119
  • 168
73
votes
6 answers

Using RestTemplate in Spring. Exception- Not enough variables available to expand

I am trying to access the contents of an API and I need to send a URL using RestTemplate. String url1 = "http://api.example.com/Search?key=52ddafbe3ee659bad97fcce7c53592916a6bfd73&term=&limit=100&sort={\"price\":\"desc\"}"; OutputPage page =…
user3311403
  • 807
  • 2
  • 7
  • 6
69
votes
10 answers

How can I tell RestTemplate to POST with UTF-8 encoding?

I'm having problems posting JSON with UTF-8 encoding using RestTemplate. Default encoding for JSON is UTF-8 so the media type shouldn't even contain the charset. I have tried to put charset in the MediaType but it doesn't seem to work anyway. My…
anssias
  • 1,994
  • 2
  • 16
  • 21
68
votes
9 answers

How to extract HTTP status code from the RestTemplate call to a URL?

I am using RestTemplate to make an HTTP call to our service which returns a simple JSON response. I don't need to parse that JSON at all. I just need to return whatever I am getting back from that service. So I am mapping that to String.class and…
john
  • 11,311
  • 40
  • 131
  • 251
62
votes
1 answer

Resttemplate getForEntity - Pass headers

Is it possible to set header as part of getForEntity method or should I use exchange? I am trying to set oauth header as part of getForEntity calls.
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
60
votes
10 answers

RestTemplate PATCH request

I have the following definition for PersonDTO: public class PersonDTO { private String id private String firstName; private String lastName; private String maritalStatus; } Here is a sample record : { "id": 1, "firstName":…
SGB
  • 2,118
  • 6
  • 28
  • 35
57
votes
6 answers

Multipart File Upload Using Spring Rest Template + Spring Web MVC

I am trying to upload a File using RestTemplate with the following code. MultiValueMap multipartMap = new LinkedMultiValueMap<>(); multipartMap.add("file", new ClassPathResource(file)); HttpHeaders headers = new…
shazin
  • 21,379
  • 3
  • 54
  • 71
57
votes
6 answers

Access Https Rest Service using Spring RestTemplate

Can anybody provide me with a code sample to access the rest service URL secured with HTTPS using the Spring Rest template? I have the certificate, username and password. Basic Authentication is used on the server-side and I want to create a client…
zdesam
  • 2,936
  • 3
  • 25
  • 32
55
votes
7 answers

Spring RestTemplate and generic types ParameterizedTypeReference collections like List

An Abstract controller class requires List of objects from REST. While using Spring RestTemplate its not mapping it to required class instead it returns Linked HashMAp public List restFindAll() { RestTemplate restTemplate =…
vels4j
  • 11,208
  • 5
  • 38
  • 63
54
votes
7 answers

What are the advantages and disadvantages of using feign over RestTemplate

I get that Feign is declarative and hence it abstracts out a lot of things for the developer. But, when should one choose one over the other? Though feign is declarative, it has serious problems with oAuth. What are some of the considerations in…
codingsplash
  • 4,785
  • 12
  • 51
  • 90