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
0 answers

getting error 'Could not extract response: no suitable HttpMessageConverter found' while using RestTemplate in Spring MVC,but works fine in Springboot

My RestTemplate code RestTemplate restTemplate = new RestTemplate(); Courses courses = restTemplate.getForObject("http://localhost:8090/apple/courses", Courses.class); If I run the same code from a SpringBoot application it works fine, but…
0
votes
1 answer

Java Spring RestTemplate getForObject response not decoded properly

I am trying to execute a HTTP GET request that I can execute properly in Postman in my Spring Boot application using the RestTemplate class. This is the code I am using to send the request: RestTemplate restTemplate = new RestTemplate(); String…
0
votes
0 answers

URIComponentsBuilder stripping out parts of URL when multiple requests come at the same time

I have a method which builds the URL for requests coming in Url = baseURL + requestXMLString UriComponentsBuilder urib = UricompoonentsBuilder.fromHttpUrl(Url) ......... ..... //restemplate call in a different class with builder as param (Cannot…
0
votes
1 answer

Submit google form by API call using RestTemplate in Java is not working

I have to submit this Google form whenever new signup is performed on my application, so I have used RestTemplate in Java Spring Boot to send API request and populate all form data as query params. These query params worked for plan text, but for…
0
votes
1 answer

Java Spring RestTemplate timeout does not work properly

I have such scenario where I do a request to an API , let's say I am doing that request using restTemplate.exchange method. I do set the timeouts like this: ((HttpComponentsClientHttpRequestFactory) requestFactory).setConnectTimeout(200090); …
iftwMZ
  • 117
  • 5
0
votes
1 answer

Issue reaching my Apigee proxy via Spring RestTemplate PUT method

I'm having a wierd issue while migrating API calls from an old webapp from our previous API management to Apigee (GCP API management tool). I'm not even sure that the fact I'm trying to call Apigee is an issue itself but still I'm mentioning it for…
Rlarroque
  • 1,900
  • 1
  • 17
  • 27
0
votes
1 answer

Passing a generic Type as responseType in RestTemplate.exchange() in Spring Rest Template

I have a candy factory that exposed various endpoints to get candies in a bag. candy such as name: m&m ; endpoint: candy-factory/mnms name: skittle; endpoint: candy-factory/skittles name: smarties; endpoint: candy-factory/smarties Api response…
barun
  • 393
  • 1
  • 5
  • 19
0
votes
0 answers

How can we send get request with data-urlencode body using RestTemplate Spring Boot

Requirement is to send notification with auth token. And to get token we have been provided below curl command. I need to implement curl equivalent code to get the token so that I can use that token while sending the notification. I have implemented…
amar
  • 1
  • 1
0
votes
3 answers

Java Set Security cookie using RestTemplate with Domain and as Secure

In addition to question below, how can I set the Cookie Domain and mark as "Secure"? Trying to set additional properties on Cookie. Setting Security cookie using RestTemplate I am trying to call a Restful JSON service using RestTemplate…
mattsmith5
  • 540
  • 4
  • 29
  • 67
0
votes
0 answers

How to resolve 400 error with Resttemplate in spring boot application?

I am trying to access external https API via my spring boot application using RestTemplate. When I send first request then I get the correct response and after that whenever send request then I get 400 bad response error. But when I restart my…
Rahul
  • 111
  • 3
  • 13
0
votes
1 answer

Error while extracting response for type [class java.lang.Object] and content type [application/octet-stream];

Here is the method that I have , what does the API that I call actually returns? This will return binary response and the response content is "application/octet-stream". The method which calls this method is from controller which will return…
Karthik
  • 371
  • 3
  • 7
  • 30
0
votes
0 answers

RestTemplate POST 403 response but HTTPEntity body is not null

Im trying to call a POST Endpoint that requires an API-Key using RestTemplate but i keep receiving a 403 Forbidden HttpClientErrorException. GET with the same authorization headers works fine so I think the API-Key auth works. The only time I get a…
0
votes
0 answers

RestTemplate 400 error for get request spring 6 apache httpclient5

When i use new instance of resttemplate like, ''' RestTemplate r = new RestTemplate();''' And hit my service,i get success. But the moment i add, '''Resttemplate r = new Resttemplate(); r.setRequestfactory(new…
0
votes
1 answer

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3

I use Spring Gateway 2021.0.8. I want to make this Post request using RestTemplate: import org.springframework.web.client.RestTemplate; public ResponseEntity getRemotePayload() { HttpHeaders headers = new…
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
0
votes
1 answer

How to use Eureka client in RestTemplate

I have the following code: import org.springframework.web.client.RestTemplate; private final RestTemplate restTemplate; public RestTemplate getRestTemplate() { return restTemplate; } ResponseEntity response =…
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
1 2 3
99
100