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
23
votes
3 answers

SpringBoot @WebMvcTest, autowiring RestTemplateBuilder

I've got a problem while testing a Spring Controller. I'm using the annotation @WebMvcTest in my test class. When I run the test, I get this error: No qualifying bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' available I'm…
Alexandre
  • 259
  • 1
  • 3
  • 6
23
votes
3 answers

How to parse gzip encoded response with RestTemplate in Spring-Web

After I modified Consuming a RESTful Web Service example to call get users by id from api.stackexchange.com I get JsonParseException: com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space…
Michal Foksa
  • 11,225
  • 9
  • 50
  • 68
23
votes
5 answers

Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONObject] and content type [application/json]

I'm digging myself in trying to send a POST request with a JSON payload to a remote server. This GET curl command works fine: curl -H "Accept:application/json" --user aaa@aaa.com:aaa "http://www.aaa.com:8080/aaa-project-rest/api/users/1" -i And…
Stephane
  • 11,836
  • 25
  • 112
  • 175
23
votes
3 answers

How to use RestTemplate efficiently in Multithreaded environment?

I am working on a project in which I need to make a HTTP URL call to my server which is running Restful Service which returns back the response as a JSON String. Below is my main code which is using the future and callables - public class…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
22
votes
1 answer

restTemplate.getforobject(),exchange(),entity() .is there any pros and cons for each method?

i have used both entity(),exchange(),getforObject(), and all seems to be working fine . but not sure which is the perfect method for different scenarios.. please give more info about each methods like pros and cons,where to use where not to use.
benny
  • 299
  • 1
  • 3
  • 8
22
votes
4 answers

RestTemplate should be static globally declared?

I am using Java Callable Future in my code. Below is my main code which uses the future and callables - public class TimeoutThread { public static void main(String[] args) throws Exception { ExecutorService executor =…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
21
votes
1 answer

Spring/RestTemplate - PUT entity to server

Please look at this simple code: final String url = String.format("%s/api/shop", Global.webserviceUrl); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); HttpHeaders…
user1209216
  • 7,404
  • 12
  • 60
  • 123
20
votes
2 answers

How to get response json from Spring WebClient

I've been trying to follow the simplest tutorials out there for how to use WebClient, which I understand to be the next greatest thing compared to RestTemplate. For example, https://www.baeldung.com/spring-5-webclient#4-getting-a-response So when I…
notacorn
  • 3,526
  • 4
  • 30
  • 60
20
votes
6 answers

How to use AsyncRestTemplate to make multiple calls simultaneously?

I don't understand how to use AsyncRestTemplate effectively for making external service calls. For the code below: class Foo { public void doStuff() { Future> future1 = asyncRestTemplate.getForEntity( …
Glide
  • 20,235
  • 26
  • 86
  • 135
20
votes
3 answers

Spring RestTemplate - async vs sync restTemplate

I wrote the following code to test the performance of both the sync RestTemplate and AsyncRestTemplate. I just ran it a few times manually on POSTMAN. We are just passing 10 references into a GET call so that we can return 10 links: RestTemplate -…
Simon
  • 19,658
  • 27
  • 149
  • 217
19
votes
6 answers

Return the List returned by ResponseEntity

My REST client uses RestTemplate to obtain a List of objects. ResponseEntitiy res = restTemplate.postForEntity(getUrl(), myDTO, List.class); Now I want to use the list returned and return it as List to the calling class. In case of string,…
ND_27
  • 764
  • 3
  • 8
  • 26
19
votes
6 answers

Spring 4.0.0 basic authentication with RestTemplate

I am currently working on integration of a third party application with our local reporting system. I would like to implement REST calls with basic authentication but facing issues in Spring 4.0.0. I have a simple solution what works nicely: final…
shippi
  • 2,344
  • 7
  • 22
  • 28
19
votes
2 answers

Spring RestTemplate post response

I'm not familiar with Spring RestTemplate. But for this project I have to use Spring RestTemplate to send a POST call to consume a rest api. I'm using this code: String restCall = restTemplate.postForObject(url+restParm, null, String.class); This…
Zamboo
  • 513
  • 2
  • 8
  • 18
18
votes
2 answers

How do I solve "Reached the maximum number of URI tags for http.client.requests" warning?

I receive this warning on my app. I am reading rfidtags from about 30 readers at the same time. Each time a tag comes in I am hitting the database to see if its in there. I have a rest api that I am using. So I use a rest template to hit the rest…
Roro
  • 427
  • 2
  • 8
  • 20
18
votes
2 answers

Integration test with TestRestTemplate for Multipart POST request returns 400

I know that similar question has been here already couple of times but following suggested fixes did not solve my problem. I have a simple controller with the following endpoint: @RequestMapping(method = RequestMethod.POST) public…
Smajl
  • 7,555
  • 29
  • 108
  • 179