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
268
votes
30 answers

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

I have been using the Spring RestTemplate for a while and I consistently hit a wall when I'am trying to debug it's requests and responses. I'm basically looking to see the same things as I see when I use curl with the "verbose" option turned on. For…
Paul Sabou
  • 3,097
  • 3
  • 16
  • 9
258
votes
6 answers

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

I want to set the value of the Accept: in a request I am making using Spring's RestTemplate. Here is my Spring request handling code @RequestMapping( value= "/uom_matrix_save_or_edit", method = RequestMethod.POST, …
edaklij
  • 4,121
  • 11
  • 31
  • 43
246
votes
15 answers

Get list of JSON objects with Spring RestTemplate

I have two questions: How to map a list of JSON objects using Spring RestTemplate. How to map nested JSON objects. I am trying to consume https://bitpay.com/api/rates, by following the tutorial from http://spring.io/guides/gs/consuming-rest/.
Karudi
  • 2,692
  • 3
  • 18
  • 19
235
votes
4 answers

How to POST form data with Spring RestTemplate?

I want to convert the following (working) curl snippet to a RestTemplate call: curl -i -X POST -d "email=first.last@example.com" https://app.example.com/hr/email How do I pass the email parameter correctly? The following code results in a 404 Not…
sim
  • 3,552
  • 5
  • 23
  • 23
187
votes
8 answers

Could not autowire field:RestTemplate in Spring boot application

I am getting below exception while running Spring Boot application during start up: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested…
Khuzi
  • 2,792
  • 3
  • 15
  • 26
175
votes
12 answers

Spring RestTemplate timeout

I would like to set the connection timeouts for a rest service used by my web application. I'm using Spring's RestTemplate to talk to my service. I've done some research and I've found and used the xml below (in my application xml) which I believe…
sardo
  • 2,731
  • 3
  • 19
  • 18
166
votes
16 answers

Spring RestTemplate exception handling

Below is the code snippet. Basically, I am trying to propagate the exception when the error code is anything other than 200. ResponseEntity response = restTemplate.exchange( url.toString().replace("{version}", version), …
vaibhav
  • 3,929
  • 8
  • 45
  • 81
141
votes
15 answers

POST request via RestTemplate in JSON

I didn't find any example how to solve my problem, so I want to ask you for help. I can't simply send POST request using RestTemplate object in JSON Every time I get: org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media…
Johnny B
  • 1,419
  • 2
  • 10
  • 3
133
votes
15 answers

Disabling SSL Certificate Validation in Spring RestTemplate

I am having two Spring-based web apps A and B, on two different machines. I want to make an HTTPS call from web app A to web app B, however, I am using a self-signed certificate in Machine B. So my HTTPS request fails. How can I disable HTTPS…
Prabhu R
  • 13,836
  • 21
  • 78
  • 112
130
votes
3 answers

HTTP get with headers using RestTemplate

How can I send a GET request using the Spring RestTemplate? Other questions have used POST, but I need to use GET. When I run this, the program continues to work, but it seems that the network is clogged because this is in an AsyncTask, and when I…
rasen58
  • 4,672
  • 8
  • 39
  • 74
117
votes
12 answers

Basic authentication for REST API using spring restTemplate

I am completely new in RestTemplate and basically in the REST APIs also. I want to retrieve some data in my application via Jira REST API, but getting back 401 Unauthorised. Found and article on jira rest api documentation but don't really know how…
shippi
  • 2,344
  • 7
  • 22
  • 28
103
votes
6 answers

Sending GET request with Authentication headers using restTemplate

I need to retrieve resources from my server by sending a GET request with some Authorization headers using RestTemplate. After going over the docs I noticed that none of the GET methods accepts headers as a parameter, and the only way to send…
special0ne
  • 6,063
  • 17
  • 67
  • 107
92
votes
3 answers

Generics with Spring RESTTemplate

I have a class like that: public class Wrapper { private String message; private T data; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { …
kamaci
  • 72,915
  • 69
  • 228
  • 366
92
votes
6 answers

WebClient vs RestTemplate

As per spring 5: WebClient is an interface representing the main entry point for performing web requests. It has been created as a part of the Spring Web Reactive module and will be replacing the classic RestTemplate in these scenarios. The new…
KayV
  • 12,987
  • 11
  • 98
  • 148
91
votes
16 answers

RestClientException: Could not extract response. no suitable HttpMessageConverter found

Using the curl command: curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/messages I am getting a JSON…
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
1
2 3
99 100