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 Boot geoserver editing/adding Styles

I wish to edit and add a new style in geoserver rest api from Spring Boot. My code gives error 500. If you know how to make it please, let me know. Of course if you have different method show it too please. package exp; import…
Piterek237
  • 61
  • 11
0
votes
1 answer

Unmarshalling Parameterized URL String Return Type with Rest Template

I'm running an application/x-www-form-urlencoded POST and getting the following as the response. response=3&responsetext=1006: The specified transaction in the request is not allowed: REJECTED CONTACT CUST…
Nimila Hiranya
  • 4,842
  • 10
  • 35
  • 52
0
votes
0 answers

Request processing failed: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : "false"

i'm working on an tokenValidationInterceptor , where a request for token validation is send to an other micro-service which communicate with keycloak and send an ok status or unautorazed status , then i add some logic to the prehandle function and…
0
votes
0 answers

How can I determine the implementation returned when mapping a JSON response to a Map with RestTemplate?

In my code, I am using RestTemplate to call an external API and receive a complex JSON response. However, for my specific use case, I only need to return the JSON as-is to the front-end. To achieve this, I have chosen to map the response to a…
0
votes
1 answer

Configure RestTemplate for AWS X-Ray

How to configure the RestTemplate to use the http client provided in the AWS X-Ray SDK ? I found an example with Spring version 2.3.4: https://github.com/aws/aws-xray-sdk-java/issues/220 . But I use Spring Boot version 3.1.0 and the example doesn't…
outdev
  • 5,249
  • 3
  • 21
  • 38
0
votes
0 answers

How to resolve connection time out error in spring boot application?

I am trying to access external API from spring boot application in my local machine using external API's username and password but every times its giving error 'org.springframework.web.client.ResourceAccessException: I/O error on POST request for…
0
votes
0 answers

TLS over RestTemplate using a publickey stored as a string

I am trying to create a RestTemplate in Spring Boot (java) that connects to an API endpoint using TLS. The catch is that I have to use a public key that I get from a custom API and have saved into a String variable. I think I need to do something…
Karthik Sankaran
  • 217
  • 2
  • 11
0
votes
0 answers

http method Patch send a request with body as null in Rest mock while it works just fine with POST

I currently have mock services setup for a few different methods (GET, POST, PATCH). I am trying to build dynamic responses via the Script option and have this working successfully for my GET and POST calls, however I am unable to get my PATCH call…
0
votes
0 answers

Getting timeout error while calling NSE India get url using Feign Client as well as Rest Template in Kotlin

This is the Url I want to call GET https://www.nseindia.com/api/holiday-master?type=trading It is working in postman and from browser as well, but when I am trying to call this api from my code in Kotlin it is throwing Read timed out. These are the…
0
votes
2 answers

Logging both ClientHttpRequestInterceptor and HttpServletRequest (or HandlerInterceptor)

I'm developing an API using Spring Boot 3.1.0 to crawl data, I called this localhost:8080/crawl. Inside this, I also use RestTemplate to call an external API called third-party.com/data. I want to track API calls both my URL and third-party…
0
votes
0 answers

Getting RestTemplate (Connection Reset) Despite everything is correct

I'm using Spring Boot RestTemplate to call REST API. On PROD, I'm getting below exception: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https:///v2/graphql": Connection reset; nested exception is…
0
votes
0 answers

Sudden Increase in Response Time with restTemplate for External API Calls

I'm experiencing an issue with the restTemplate while making requests to an external API in my service. Most of the time, the API calls are functioning normally with an average response time of 2-3ms. However, occasionally, the response time…
0
votes
0 answers

Spring boot RestTemplate handle different responseType and get error message from third-party API

I wrote a Service in Spring Boot that executes the getForObject(URI url, Class responseType) method. I implemented a dedicated RestTemplate, the restTemplateWithErrorHandler, to handle any errors: @Configuration public class…
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131
0
votes
0 answers

RestTemplate HttpServerErrorException from POST

I am getting below error while making a POST call to external service. I am not setting any cookie in Headers while making request. Please help. org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error:…
0
votes
1 answer

Call to GraphQL Query with RestTemplate is giving either 400 or invalid syntax message

I have been experiencing a weird issue trying to call a GraphQL query using RestTemplate from the SpringBoot framework. I say it is weird because it is only happening with this particular query. This is the code I am using (because that is the…
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
1 2 3
99
100