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
11
votes
1 answer

Resttemplate form/multipart: image + JSON in POST

I'm trying to call a rest ws (using resttemplate), that accepts an image and some JSON. However, I don't seem to be able to get it running. The relevant code is as follows: HttpHeaders header = new HttpHeaders(); …
TamasGyorfi
  • 587
  • 1
  • 6
  • 14
10
votes
3 answers

RestTemplate basic or digest Authentication with the current httpclient (4.x)

I'm trying to do Digest mostly (or Basic) Authentication using RestTemplate and httpclient (4.x). Since I couldn't find any relevant examples of how to actually do this, I have attempted various ways to hook the various httpclient artifacts, with…
10
votes
3 answers

Setting Security cookie using RestTemplate

I am trying to call a Restful JSON service using RestTemplate and Jackson json convertor. Now in order to call the service I need to pass in a Security cookie. I can achieve this by using URLConnection (See the code below) URL url= new…
user220751
10
votes
1 answer

Spring RestTemplate implementation vs RestOperations interface

It seems everywhere around the web there are examples of people autowiring the implementation class RestTemplate rather than it's interface RestOperations. Even in the Spring manuals and documentation the interface is said to be "not often used".…
Toofy
  • 804
  • 9
  • 19
10
votes
3 answers

How can we make asynchronous REST api call in Java?

I am using Spring RestTemplate and want to make a call to another service that doesn't return any response body. So, I don't want to wait for the response. So, it's just fire and forget, and continue with the remaining code. I am thinking of…
user10937286
  • 321
  • 1
  • 3
  • 10
10
votes
2 answers

Ribbon load balancer with webclient differs from rest template one (not properly balanced)

I've tried to use WebClient with LoadBalancerExchangeFilterFunction: WebClient config: @Bean public WebClient myWebClient(final LoadBalancerExchangeFilterFunction lbFunction) { return WebClient.builder() .filter(lbFunction) …
10
votes
3 answers

Why is Spring de-coding + (the plus character) on application/json get requests? and what should I do about it?

I have a Spring application that receives a request like http://localhost/foo?email=foo+bar@example.com. This triggers a controller that roughly looks like this: @RestController @RequestMapping("/foo") public class FooController extends Controller…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
10
votes
5 answers

RestTemplate set timeout per request

I have a @Service with several methods, each method consumes a different web api. Each call should have a custom read timeout. Is it thread-safe to have one RestTemplate instance and change the timeout via the factory in each method like…
prettyvoid
  • 3,446
  • 6
  • 36
  • 60
10
votes
2 answers

RestTemplate can't fetch JSONObject

I can't fetch JSONObject directly, this code works: RestTemplate restTemplate = new RestTemplate(); String str = restTemplate.getForObject("http://127.0.0.1:8888/books", String.class); JSONObject bookList = new JSONObject(str); but this code…
EralpB
  • 1,621
  • 4
  • 23
  • 36
10
votes
2 answers

Spring RestTemplate: SSL handshake failure

I am trying to consume a restful ws with basic auth. I did not import any cert into my keystore. When I use chrome plugin Advance Rest client to test it (using basic auth with base64 encoded username:pass). I can see the response back. So far so…
David
  • 3,538
  • 9
  • 39
  • 50
10
votes
4 answers

Spring RestTemplate: sending array / list of String in GET request

I'm trying to send a array / list of String to my REST server through Spring RestTemplate. This is on my android side: private List articleids = new ArrayList<>(); articleids.add("563e5aeb0eab252dd4368ab7"); …
Simon
  • 19,658
  • 27
  • 149
  • 217
10
votes
1 answer

Spring Rest Client want to see error message instead of exception

I have a spring rest client. when authentication details are not provided in the headers and I hit the service with ResponseEntity resp = restTemplate.exchange(url, HttpMethod.GET, request, String.class); I get exception : invoking error…
Amar
  • 1,556
  • 3
  • 18
  • 28
10
votes
2 answers

How to receive application/pdf response from a server using RestTemplate

I am trying capture the response of an HTTP request made by my java client code. The response has a content-type of application/pdf. In the logs I can see that the server sent a response in Object result = getRestTemplate().postForObject(urlString,…
ak1984
  • 373
  • 1
  • 3
  • 10
10
votes
3 answers

java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava

I use very simple code that returns XML RestTemplate restTemplate = new RestTemplate(); Source oResponse = restTemplate.postForObject(url, entity, Source.class, vars); XPathOperations xpathTemplate = new Jaxp13XPathTemplate(); String…
Vladimir
  • 630
  • 3
  • 12
  • 26
10
votes
0 answers

How to setup an Android development environment using STS, Android SDK, Maven and Spring Android

Install latest STS (currently 3.4.0) Setup your Android development environment in STS as per the normal Android development setup steps for an existing IDE: http://developer.android.com/sdk/installing/index.html For the above step, you could save…
dleerob
  • 4,951
  • 4
  • 24
  • 36