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

How do I get Spring REST Template to log the JAXB marshalled XML ?

Using Spring REST Template to make requests. Springs internal JAXB converter takes my JAXB pojo and marshals it to xml for the request body. How do I get this XML into the logs for debugging purposes?
chad
  • 7,369
  • 6
  • 37
  • 56
1
vote
0 answers

Spring MVC + Spring Security + JAAS + RestTemplate

I have to design a DMS (document management system) and I decided to use Spring as base framework for the Platform I'm going to build. I'm designing a Platform that is comprised of 2 main application: engine app and portal app. Both are web…
Nicola
  • 2,876
  • 2
  • 18
  • 26
1
vote
1 answer

Android App force closes on connection refuse

i am trying to create an application which uses rest template for login. Code works fine when tomcat is up and running with application. when i stop tomcat, application trows "connection refuse exception" and application closed. What i want is "how…
Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79
1
vote
1 answer

Rest Template Spring Android PUT with Raw content type

I am trying to fix an issue with my RestTemplate PUT request. Basically, the server expects data(an object) to be put in "Raw" content-type but as xml stream. I tried many combinations(of converter, content-type etc..) but nothing helps. I either…
Kaps
  • 2,345
  • 2
  • 26
  • 37
1
vote
0 answers

"Raw" content type support in Spring Android for custom object serialized to xml in PUT

I have a situation, I am using spring andorid apis to do PUT operation. The service(back end) expects xml represenatation of a custom object. And the Content-Type as "Raw". I am using SimpleXmlHttpMessageConverter() to serialize the custom object to…
Kaps
  • 2,345
  • 2
  • 26
  • 37
1
vote
0 answers

Jackson subclasses deserialization in Android: invalid type id and no class found

I'm trying to get deserialization of subclasses working in Jackson. I defined a simple POJO and some sublcasses, but I'm getting the following exception when deserializing: 06-25 10:06:32.765: E//RequestProcessor.java:251(26690): Caused by:…
user1781028
  • 1,478
  • 4
  • 22
  • 45
1
vote
1 answer

getting JsonMappingException while mapping nested enum

Im using spring restTemplate and converting the json response to pojo. But getting a JsonMappingException POJO @Root public class Account { @Element private int id; @Element private int customerId; @Element private AccountType…
user1824107
  • 15
  • 1
  • 5
1
vote
2 answers

Spring MVC REST Service and Client. I am looking for a better way to deal with errors

Spring MVC REST Service and Client. I am looking for a better way to deal with errors! I would like sometimes to return a errror message to the client or a status code but I dont know hiow. Can someone please tell me how to find a better way of…
user2428795
  • 547
  • 6
  • 11
  • 26
1
vote
0 answers

How to use piped multi value query parameters with Spring's Rest Template or URI Builder

I'm looking for a Spring or Apache solution that separates multiple query parameters with a piped character. I'll even take a library that reduces an array or collection of strings into a single string that is separated with a piped…
Dan
  • 1,179
  • 2
  • 10
  • 18
1
vote
0 answers

Send serialized JSON in restTemplate postForObject

Here is the deal. I have a postForObject to sent. In that postForObject, i must send 2 objects: A JSON OBJECT WITH NAME "DATA" (e.g: data={"client":"name","id":"3"}) A STRING WITH NAME "SESSION_TOKEN" (e.g: session_token="abc") I've tried so many…
Alexandre
  • 751
  • 2
  • 11
  • 27
1
vote
2 answers

Restful Service in Spring results in 400 Bad request

I'm having some problems using the Spring restTemplate. According to API documentation (ONLY FOR PHP) of Rest Service that i'm trying to access. The correct format of POST are: curl -d…
Alexandre
  • 751
  • 2
  • 11
  • 27
1
vote
1 answer

POST request with Spring RestTemplate- BadRequest 400

Hope a Spring guru could help me on this, Iam developing a multi web servces application, All these Web Services are based on a Jar called based-server, which got all base classes which can be inherited where ever required. So this base-server…
bluelabel
  • 1,996
  • 6
  • 29
  • 44
1
vote
0 answers

RestTemplate MalformedByteSequenceException Invalid byte 2 of 3-byte UTF-8 sequence

I am trying to load an object from a webservice, using RestTemplate, like this: Menu menu = null; RestTemplate rest = new RestTemplate(); String request = "
1
vote
1 answer

RestTemplate Post UTF-8 data with setContentType(MediaType.MULTIPART_FORM_DATA)

I'm using spring for android. I want to create registration form in my android app. The users maybe fill out the form with utf-8 format but when data send to server and store in MySQL database I find ?????? in my database field.table's column type…
Misagh Aghakhani
  • 1,023
  • 2
  • 13
  • 29
1
vote
2 answers

android resttemplate +httpclient header encoding problems

Android client, using Springs resttemplace and the Apache common HTTP client to make requests. I'm working against a server, that sometimes returns a 401 error, with a http header string, "ERROR" that contains a user-info string. The string is…
Mathias
  • 3,879
  • 5
  • 36
  • 48