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
18
votes
5 answers

Download Large file from server using REST template Java Spring MVC

I have a REST service which sends me a large ISO file ,there are no issues in the REST service . Now I have written a Web application which calls the rest service to get the file ,on the client(web app) side I receive a Out Of memory Exception.Below…
arpit joshi
  • 1,987
  • 8
  • 36
  • 62
18
votes
3 answers

Spring RestTemplate to POST request with Custom Headers and a Request Object

In Spring RestTemplate is there a way to send Custom Headers together with a POST Request Object. I have already tried out the exchange method which is available. It seems that we can send key value pairs together with a custom headers but not a…
MCF
  • 768
  • 2
  • 6
  • 21
17
votes
2 answers

Best practices on rest client using spring RestTemplate

I have read some tutorials about implementing REST client in java web application that use SPRING to manage beans. Every example I found, every time doing a REST request it creates new RestTemplate. Normally web applications use singleton spring…
Chamly Idunil
  • 1,848
  • 1
  • 18
  • 33
17
votes
1 answer

How to call HTTPS restful web services using Spring RestTemplate

I am using Tomcat7, Spring framework for ReST web services. I am trying to call an https web service using Spring RestTemplate. I am getting the following error: unable to find valid certification path to requested target; nested exception is…
user1272936
  • 299
  • 1
  • 4
  • 15
17
votes
2 answers

Using Proxy with HttpComponentsClientHttpRequestFactory and RestTemplate

Can some one guide me how can I configure HttpComponentsClientHttpRequestFactory to use proxy server. All examples I have seen are using SimpleClientHttpRequestFactory.
16
votes
2 answers

RestTemplate not passing Origin header

I'm trying to make a cross-origin request using Spring's RestTemplate. The communication is done between two Spring-boot webapps, both running on localhost but different port. What I do is: HttpHeaders httpHeaders = new…
Krzysztof Ś
  • 173
  • 1
  • 5
16
votes
1 answer

Spring TestRestTemplate vs RestTemplate

What is the difference between RestTemplate and its test version? When we do exception handling through @ControllerAdvice, RestTemplate is throwing the exception, but for same flow test version is returning json containing exception details. So, I…
krmanish007
  • 6,749
  • 16
  • 58
  • 100
16
votes
7 answers

Ribbon with Spring Cloud and Eureka: java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local

I am working on Spring Boot Eureka Client Application with Ribbon Load Balancer. I have two instances of the server registered with Eureka with the name "TEST". On the client side, I have the following code to fetch the server from…
15
votes
4 answers

Spring Boot random "SSLException: Connection reset" in Kubernetes with JDK11

Context: We have a Spring Boot (2.3.1.RELEASE) web app It's written in Java 8 but running inside of a container with Java 11 (openjdk:11.0.6-jre-stretch). It has a DB connection and an upstream service that is called via https (simple…
Urosh T.
  • 3,336
  • 5
  • 34
  • 42
15
votes
1 answer

Why does RestTemplate consume excessive amounts of memory?

Question Why does Spring's RestTemplate use an excessive amount of heap (particularly the G1 Old Generation) when sending a file. Context We observed the RestTemplate to consume excessive amounts of memory when sending files via POST requests. We…
Trinova
  • 443
  • 6
  • 18
15
votes
3 answers

Spring RestTemplate - Need to release connection?

This is my Configuration for Rest Template, @Bean @Qualifier("myRestService") public RestTemplate createRestTemplate(@Value("${connection.timeout}") String maxConn) { PoolingHttpClientConnectionManager connectionManager = new…
Umar
  • 1,002
  • 3
  • 12
  • 29
15
votes
4 answers

How to enforce TLS1.2 to Rest client using Rest Template

I am consuming json webservice using Spring3.0 restTemplate by calling post method. MultiValueMap headers = new LinkedMultiValueMap(); headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE); …
Panther
  • 3,312
  • 9
  • 27
  • 50
15
votes
3 answers

How to download image using rest template?

I have the following code: restTemplate.getForObject("http://img.championat.com/news/big/l/c/ujejn-runi_1439911080563855663.jpg", File.class); I especially took image which doesn't require authorization and available absolutely for all. when…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
15
votes
4 answers

How to write mockito junit for Resttemplate exchange method

How to write mockito junit for the method below: @Autowired RestTemplate restTemplate; ResponseEntity execute(final String url, HttpMethod httpMethod, HttpEntity entityRequest, String.class,…
Java Learing
  • 269
  • 3
  • 11
  • 26
15
votes
2 answers

Spring RestTemplate - Overriding ResponseErrorHandler

I am calling a ReST service through RestTemplate and trying to override ResponseErrorHandler in Spring 3.2 to handle custom error codes. CustomResponseErrroHandler public class MyResponseErrorHandler implements ResponseErrorHandler { @Override …
user3670450
  • 151
  • 1
  • 1
  • 4