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

How to use Spring RestTemplate and JAXB marshalling on a URL that returns multiple types of XML

I need to make a Rest POST to a service that returns either a or an and always status code 200. (lame 3rd party product!). I have code like: Job job = getRestTemplate().postForObject(url, postData, Job.class); And my…
Tom
  • 2,416
  • 3
  • 31
  • 38
13
votes
3 answers

Mocking RestTemplateBuilder and RestTemplate in Spring integration test

I have a REST resource that gets a RestTemplateBuilder injected to build a RestTemplate: public MyClass(final RestTemplateBuilder restTemplateBuilder) { this.restTemplate = restTemplateBuilder.build(); } I would like to test that class. I need…
user3629892
  • 2,960
  • 9
  • 33
  • 64
13
votes
2 answers

Spring RestTemplate message converter priority when posting

What is the most convenient way to influence the priority of the message converters Spring applies when POSTing with RestTemplate? Use case: I want to ensure a given entity is POSTed as JSON rather than e.g. XML when I do…
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
13
votes
3 answers

Junit5 @ParameterizedTest How to pass array as one of parameter

I have a test in which I want to pass three parameters: String Enum Array of Strings Example: @ParameterizedTest @CsvSource({ "/path/to/first/file.xlsx, FIRST, {THIRD PARAMETER SHOULD BE ARRAY OF STRINGS}", …
13
votes
3 answers

Spring RestTemplate follow redirect with cookie

Recently I ran into a problem where I needed to do a GET request to a remote service (Using a simple servlet i presume), and RestTemplate returned Too many redirects!. After some investigation, it seems like the first request made to the specified…
Robin Jonsson
  • 2,761
  • 3
  • 22
  • 42
13
votes
1 answer

How to cancel AsyncRestTemplate HTTP request if they are taking too much time?

Since starting, I was always confuse of how to deal with InterruptedException and how to properly cancel the http request if they are taking too much time. I have a library in which I have provided two methods, sync and async for our customer. They…
john
  • 11,311
  • 40
  • 131
  • 251
13
votes
8 answers

Map Nested JSON Objects to Java Classes with Spring RestTemplate

I know this may be simple. However, I just can't get it to work. So I am trying to use Spring RestTemplate to map my JSON data. I have following JSON response from a rest call. { "message":"ok", "status":"ok", "data":[ {"Name":"Yo", …
Ren
  • 131
  • 1
  • 1
  • 3
13
votes
1 answer

Deserializing JSON with multiple types in one field

I would like to deserialize JSON (with Jackson 1.9.11 and RestTemplate 1.0.1), in which one field may have more type meanings, for example: {"responseId":123,"response":"error"} or {"responseId":123,"response":{"foo":"bar", ... }} Either…
shmoula
  • 1,053
  • 2
  • 10
  • 33
13
votes
2 answers

RestTemplate and Cookie

I need to send an HTTP cookie, I'm using RestTemplate: HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("Cookie", "SERVERID=c52"); HttpEntity requestEntity = new HttpEntity(null, requestHeaders); ResponseEntity responses =…
Ilkar
  • 2,113
  • 9
  • 45
  • 71
12
votes
2 answers

RestTemplate GET request with request params

I have to call a REST webservice and I am planning to use RestTemplate. I looked at examples on how to make a GET request and they are as shown below. String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}",…
Chandra
  • 1,577
  • 3
  • 21
  • 28
12
votes
4 answers

Custom RestTemplate using requestFactory of RestTemplateBuilder in SpringBoot 2.1.x is not backward compatible with version 1.5.x

In Spring Boot 1.5.x, I was creating a custom RestTemplate like below: @Bean public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { PoolingHttpClientConnectionManager poolingConnectionManager = new…
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
12
votes
2 answers

Spring Boot RestTemplate post without response type

I need to call another api and I am trying to use postForEntity as below: restTemplate.postForEntity(postUrl, request, responseType) Now, the api that I am calling is returning an object of a particular type, let's say CustomerDto. But I am not…
T Anna
  • 874
  • 5
  • 21
  • 52
12
votes
1 answer

How to inject RestTemplate

I am not using xml configurations to define beans. Instead using component scanning and autowire to define and inject dependencies. RestTemplate is part of springframework. How can I inject this class ?
Ravi
  • 323
  • 2
  • 6
  • 18
12
votes
1 answer

TradeOff between creating a new RestTemplate each Time and Creating a Single Class returning same Rest Template

In Spring Boot,for making a Rest Client call currently creating RestTemplate using the new keyword in multiple classes. RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.exchange( Planning to create a…
Rehan
  • 929
  • 1
  • 12
  • 29
12
votes
2 answers

How to set RequestConfiguration per request using RestTemplate?

I have a library which is being used by customer and they are passing DataRequest object which has userid, timeout and some other fields in it. Now I use this DataRequest object to make a URL and then I make an HTTP call using RestTemplate and my…
user1950349
  • 4,738
  • 19
  • 67
  • 119