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

Spring RestTemplate Dependency Error

I am trying to implement Spring RestTemplate into my exsting Spring REST Web application. I tried that: ... private final String BASE_URL = "http://localhost:8080/a"; private final RestTemplate restTemplate = new RestTemplate(); ... Map
kamaci
  • 72,915
  • 69
  • 228
  • 366
0
votes
0 answers

Spring6 how to configure apache http async client in Resttemplate

I was referring the below code from a repository(https://github.com/springframeworkguru/mssc-brewery-client/blob/apache-client/src/main/java/guru/springframework/msscbreweryclient/web/config/NIORestTemplateCustomizer.java) for learning purpose.…
ashok
  • 1,078
  • 3
  • 20
  • 63
0
votes
0 answers

POST request with List of MultipartFile

I'm trying to perform POST request with Multipart Form Data. I have this controller receiving request @PostMapping(value = "/photos", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @Operation(summary = "Accepts photo. Adds photo to queue…
BANTYC
  • 151
  • 1
  • 9
0
votes
1 answer

Getting org.mockito.exceptions.misusing.PotentialStubbingProblem: Strict stubbing argument mismatch while using restTemplate

I call external API by using RestTemplate's exchange() in my application. And currently, I am writing junit and mockito test case for restTemplate call but I am getting stubbing argument mismatch exception. Here is my code @Service public class…
Rahul
  • 111
  • 3
  • 13
0
votes
0 answers

In Spring Boot 2, using the RestTemplate, how do I set a default read timeout?

I'm using Spring Boot 2. I have @Value("${my.read.timeout:-1}") private Integer timeout; @Bean public RestTemplate getRt() { RestTemplate rt = new RestTemplate(getFactory()); return rt; } private HttpComponentsClientHttpRequestFactory…
Dave
  • 15,639
  • 133
  • 442
  • 830
0
votes
0 answers

Http Request from Spring Boot 3 Scheduled Jobs

After the migration to Spring Boot 3 I'm having errors with @Scheduled jobs that made http request to other microservices with RestTemplate. When an http request it's made, a custom Interceptor starts: @Override public ClientHttpResponse…
Miche
  • 23
  • 1
  • 7
0
votes
0 answers

RestTemplate not working in Spring Boot 3. I am migrating from Spring Boot 2.x to 3.x

I have RestTemplate configured to communicate with two services, but as it is maintenance mode, can we use RestTemplate in Spring Boot 3? One of the implementations, an end point which is configured through RestTemplate, is not working I am unable…
0
votes
0 answers

Marshal POJO to XML Without DTO level tag in Spring Framework

Within the Spring framework, I am looking use RestTemplate (yes I know rest template isn't ideal but the infrastructure is already in place) to POST a DTO DtoRequest with parameters field1, field2, and field3. When the request body is marshalled, it…
0
votes
0 answers

getResponseBody(ClientHttpResponse) not quite working in DefaultResponseErrorHandler

DefaultResponseErrorHandler has some methods, but I would like to point out only a few of them. From source code: /** * Template method called from {@link #hasError(ClientHttpResponse)}. *

The default implementation checks {@link…

IceMajor
  • 37
  • 5
0
votes
0 answers

How to make RestTemplate throw all custom exceptions, also the ones of 2xx codes

I can't get my RestTemplate to throw all the exceptions it encounter on its way down the controller's endpoint. To make my thought more clear, I'll provide an example. Simple endpoint PATCH /api/entity/{entity_id} which requires a body in form of…
IceMajor
  • 37
  • 5
0
votes
1 answer

spring kotlin restTemplate - how to parse JSON array inside response

I keep struggling with parsing response represented by a JSON array into a data class entity using Spring with Kotlin and RestTemplate. Non-array like responses are without an issue, ie. the following is just enough to work as expected: // JSON…
David Zaba
  • 67
  • 1
  • 9
0
votes
0 answers

spring-security-oauth2-client broken pipe during getting the token

The problem is we use spring-security-oauth2-client 5.3.4 in order to get the authorization token from the external autorization server and each first request (when there is no token in memory) there is an error comes with SocketException thrown:…
0
votes
0 answers

Curl Vs Spring's RestTemplate exchange method huge difference in performance

Under what circumstances a Restful call made with curl would be much faster than Spring's RestTemplate I have to use Spring's RestTemplate's exchange methods as I am sending a JWT token as an Http Header (amongst others). The URL sits behind a DNS…
user721025
  • 172
  • 5
  • 15
0
votes
0 answers

InvalidUseOfMatchersException when using restTemplate exchange method

I am getting InvalidUseOfMatchersException while running the test case given below import org.mockito.junit.MockitoJunitRunner; import static org.mockito.Mockito.when; import static…
LearnToCode
  • 169
  • 1
  • 5
  • 19
0
votes
1 answer

Spring RestTemplate does not support streaming

There is a number of anwsers for the question on how to make RestTemplate stream the request. Why does RestTemplate consume excessive amounts of memory? POST InputStream with RestTemplate It looks like simple by setting the buffering to false on the…
marcel
  • 377
  • 2
  • 9