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

Using sping's restTemplate with a timeout, how do I detect a timeout?

I've initialized my restTemplate as follows: HttpClient httpClient = HttpClientBuilder.create().build(); HttpComponentsClientHttpRequestFactory requestFactory = new…
linuxdan
  • 4,476
  • 4
  • 30
  • 41
30
votes
1 answer

Spring RestTemplate postForObject with Header: webservice can't find my header parameters

I'm struggling with RestTemplate. I need to POST some authentication information to a rest webservice. I can send a request and I get a response. But according to the response my header parameters are not getting through. (Sending the same request…
POL
  • 313
  • 1
  • 3
  • 5
28
votes
5 answers

How to add base URI in RestTemplate

Is there any other way to initialize RestTemplate with base URI other than extending RestTemplate and overriding the execute method.currently i have the code like below.Thanks class CustomRestTemplate extends RestTemplate { String…
Amit Swain
  • 301
  • 1
  • 6
  • 11
28
votes
3 answers

How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate

I want to use RestTemplate/TestRestTemplate by including the artifact in a SpringBoot application org.springframework spring-web But this automatically…
datree
  • 483
  • 3
  • 7
  • 14
27
votes
7 answers

spring resttemplate url encoding

I try to do a simple rest call with springs resttemplate: private void doLogout(String endpointUrl, String sessionId) { template.getForObject("http://{enpointUrl}?method=logout&session={sessionId}", Object.class, endpointUrl,…
user1145874
  • 959
  • 3
  • 13
  • 21
27
votes
4 answers

Get STRING response from restTemplate.put

I'm having a problem using Spring restTemplate. For now i'm sending a PUT request for a restful service and that restful service send me back important informations in response. The question is that restTemplate.put are a void method and not a…
Alexandre
  • 751
  • 2
  • 11
  • 27
27
votes
1 answer

How to pass key value pair using resttemplate in java

I've to pass key value pair in the body of post request. But when I run my code, I get the error as "Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type…
Abhinandan Sharma
  • 273
  • 1
  • 3
  • 5
26
votes
2 answers

CustomDeserializer has no default (no arg) constructor

I am consuming a REST Api with RestTemplate. The response I'm getting from the API has lots of nested objects. Here's a little snippet as an example: "formularios": [ { "form_data_id": "123006", "form_data": { "form_data_id":…
abril
  • 363
  • 1
  • 3
  • 4
26
votes
8 answers

Need to ignore certificate when using restTemplate

I am trying to send a request to following address. The certificate is not valid and I would like to ignore it. I wrote following code based on my research on 1, 2 but I am not able to complete it. I am using Java…
Daniel Newtown
  • 2,873
  • 8
  • 30
  • 64
25
votes
1 answer

Getting "400 This page expects a form submission" when making a rest call to trigger a Jenkins Job

I need to trigger a Jenkins Job from my Java code.The Jenkins API expects a application/x-www-form-urlencoded Content-Type and I am able to trigger the job (using Basic AUTH) from Postman Rest Client.However When I try to the same from my java…
soumitra goswami
  • 818
  • 6
  • 29
25
votes
2 answers

Is there a way to pass header information in Spring RestTemplate DELETE call

In Spring RestTemplate we are having the following methods for delete. @Override public void delete(String url, Object... urlVariables) throws RestClientException { execute(url, HttpMethod.DELETE, null, null, urlVariables); } …
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
25
votes
4 answers

How to set content-type for the file in multipart upload when using RestTemplate (from a rest client)

The file i'm trying to upload will always be a xml file. I want to set the content-type as application/xml Here is my code: MultiValueMap
RGR
  • 681
  • 1
  • 6
  • 9
24
votes
5 answers

What's the proper way to escape URL variables with Spring's RestTemplate when calling a Spring RestController?

When calling RestTemplate.exchange to do a get request, such as: String foo = "fo+o"; String bar = "ba r"; restTemplate.exchange("http://example.com/?foo={foo}&bar={bar}", HttpMethod.GET, null, foo, bar) what's the proper to have the URL variables…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
24
votes
2 answers

POST InputStream with RestTemplate

I have a client that needs to POST a large number of large json files to a server. I've been able to get it working by reading each of the files into memory and posting the entire file with RestTemplate. However, the client quickly runs out of…
Tom
  • 3,006
  • 6
  • 33
  • 54
24
votes
2 answers

RestTemplate to NOT escape url

I'm using Spring RestTemplate successfully like this: String url = "http://example.com/path/to/my/thing/{parameter}"; ResponseEntity response = restTemplate.postForEntity(url, payload, MyClass.class, parameter); And that is good. However,…
huwr
  • 1,720
  • 3
  • 19
  • 34