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

Default keep-alive time for a HttpConnection when using Spring Rest Template

I am trying to know how long a HttpConnection is kept alive when inactive, before a new connection is created via Spring rest Template. I looked at default Connection Time-Out and Read Time-Out parameters, but I believe these are used in the…
sam
  • 133
  • 1
  • 1
  • 11
12
votes
1 answer

How do I remove certain HTTP headers added by Spring's RestTemplate?

I'm having a problem with a remote service I have no control over responding with HTTP 400 response to my requests sent using Spring's RestTemplate. Requests sent using curl get accepted though, so I compared them with those sent through…
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
12
votes
3 answers

How to decide optimal settings for setMaxTotal and setDefaultMaxPerRoute?

I have a RestService running on 45 different machines in three datacenters (15 in each datacenter). I have a client library which uses RestTemplate to call these machines depending on where the call is coming from. If the call is coming from DC1,…
john
  • 11,311
  • 40
  • 131
  • 251
12
votes
3 answers

Spring restTemplate issue in getting response

My rest server is generating response when I called it with rest client software. When I call it with resttemplate code mentioned above, then server generates response(print logs) but resttemplate does nothing(no next line executes after call) and…
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
12
votes
2 answers

SimpleClientHttpRequestFactory vs HttpComponentsClientHttpRequestFactory for Http Request timeout with RestTemplate?

I am working on a project in which I need to make a HTTP URL call to my server which is running Restful Service which returns back the response as a JSON String. Below is my main code which is using the future and callables: public class…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
12
votes
1 answer

Deserializing Nested objects using RestTemplate

I am using RestTemplate and having issues deserializing an object. Here is what I am doing. The JSON response looks like, { "response": { "Time": "Wed 2013.01.23 at 03:35:25 PM UTC", "Total_Input_Records": 5, },- …
nilesh
  • 14,131
  • 7
  • 65
  • 79
12
votes
2 answers

Howto use Spring Resttemplate with JSON only

I have a rest-service which provides information in XML or JSON. I connect my application to this service with Spring Resttemplate. Unfortunately my responses are all in XML instead of the prefered JSON format. My analysis of the requests is, that…
Adrian
  • 505
  • 1
  • 6
  • 19
11
votes
2 answers

RestTemplate with Basic Auth in Spring 3.1

We were using RestTemplate with xml configuration in Spring 3.0 and it was working perfectly fine.
11
votes
4 answers

How to configure Spring RestTemplate with SSL (in Spring @MVC)

I want to configure my Spring @MVC stub application's Spring RestTemplate with SSL for communicate to REST base https application, that deployed on Tomcat server (Spring 3, Tomcat 7). I have done up to now my works by refer this link. Now I have not…
Channa
  • 4,963
  • 14
  • 65
  • 97
11
votes
1 answer

Spring Android: using RestTemplate with https and cookies

I need use cookies on an https connection from an android native app. I am using RestTemplate. Checking other threads (eg. Setting Security cookie using RestTemplate) I was able to handle cookies within an http…
virtual82
  • 855
  • 1
  • 10
  • 20
11
votes
3 answers

How to solve the error "No serializer found for class java.io.ByteArrayInputStream " when passing MultipartFile using RestTemplate?

I am trying to pass a spring MultipartFile from my application to a microservice, and using RestTemplate, as the following, HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); …
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
11
votes
1 answer

Spring RestTemplate SocketException Connection reset on quick consecutive executions

Setup Simple server and client applications running locally. Server endpoint receives a POST request with a sleep time to simulate work. Client is a SpringBoot app using RestTemplate for HTTP calls. Simulating a 500ms delay at the server per request…
frpet
  • 155
  • 1
  • 9
11
votes
1 answer

Request by restTemplate to Netty Server hangs the thread

RestTemplate example is below. public class SimpleClient { private final String URL; private AsyncRestTemplate rest = new AsyncRestTemplate(new Netty4ClientHttpRequestFactory()); private RestTemplate restTemplate = new…
Gurkan İlleez
  • 1,503
  • 1
  • 10
  • 12
11
votes
2 answers

Resttemplate and patch, invalid

I use spring 1.4.3 I try to call a web service @PatchMapping(value = "/members/{memberId}/card") public ResponseEntity updateMemberCardId(@PathVariable("memberId") Long memberId, @RequestBody String cardId) throws ResourceNotFoundException { …
robert trudel
  • 5,283
  • 17
  • 72
  • 124
11
votes
2 answers

Spring RestTemplate: Exponential Backoff retry policy

I'm reading up on GCM: https://developers.google.com/cloud-messaging/server and one of the requirements is that the server needs to be able to: handle requests and resend them using exponential back-off. I use Spring RestTemplate for my backend…
Simon
  • 19,658
  • 27
  • 149
  • 217