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

Spring and Jackson Mapper Can not deserialize instance of CUSTOM_CLASS out of START_OBJECT

So I'm getting the error in the title and I'm using the following code: ... RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add( new MappingJacksonHttpMessageConverter()); String url =…
Alexandr
  • 708
  • 9
  • 29
1
vote
1 answer

Wrong headers when trying to GET image

I am trying to fetch an image using Spring for Android and Robospice. I have my SpiceService set up with a createRestTemplate method as per the various samples on GitHub and this is were I set my headers. I also set Gson, Form and StringHttp message…
Qw4z1
  • 3,041
  • 1
  • 24
  • 36
1
vote
1 answer

Not clear why error happened, is due to incorrect usage of AsyncTask?

Not clear why error happened, is due to incorrect usage of AsyncTask? I'm now developing an android calendar app. I want to have synchronize function which allow user can synchronize their data to server or vice verse. When user open the app, it…
Le Nghi
  • 23
  • 6
1
vote
3 answers

Can not get RestTemplate to work properly. Ends up in 406 Not Acceptable

I'm trying to use Spring's RestTemplate to implement a payment provider into a project I'm working on. The XML being returned from the payment provider is as follows: 0031
Ron Pasch
  • 231
  • 2
  • 5
1
vote
3 answers

Android with Spring Resttemplate: Error messageConverter

i am developing android application with maven and spring resttemplate. This is my pom.xml 2.3.3 1.0.0.RELEASE
Josymar De Leon
  • 147
  • 4
  • 11
1
vote
1 answer

Is there a limit size of Spring RestTemplate postForObject?

For example, if the post url is: http://www.wolf.com/pcap/search?stime={stime}&etime=${etime}&bpf=${bpf} then can we do this: Map vars = new HashMap(); vars.put("bpf", bpf); ... responseString =…
hongchangfirst
  • 245
  • 3
  • 17
1
vote
0 answers

Spring's RestTemplate and prefix in JSON body

I'd like to use RestTemplate to obtain response from server and process that response in my Android app, but server answers with prefix (or variable) in json body, so response looks similar to this: response={"foo":"bar"} Is it possible to omit…
shmoula
  • 1,053
  • 2
  • 10
  • 33
1
vote
0 answers

Spring for Android 1.0.0.RC1 : no suitable HttpMessageConverter found

I'm using Spring for Android for some time in my android app and I use Gson for the marshalling/unmarshalling. I decided to update Spring for Android with the 1.0.0.RC1 version instead of 1.0.0.M1 and now I get a no suitable HttpMessageConverter…
dst17
  • 11
  • 2
1
vote
1 answer

Spring RestTemplate postForObject call within Rest service

I'm currently working on a Rest-Service that needs to call another Rest-Method provided on the same server. So for example the method on path /myPath/foo needs to request something from the method at path /myPath/restApi/bar How can I do that with…
andy
  • 1,852
  • 2
  • 20
  • 31
1
vote
1 answer

Spring Web - Restful Webservice - pass/receive ArrayList as an argument/parameter in client/server side

I have created a sample application to get full idea of Spring MVC with REST Webservice. I have created an application which host webservice and a client which calls to this webservice and fetch the relevant data. I am able to pass the arguments…
javakid1212
  • 63
  • 2
  • 8
1
vote
1 answer

RestTemplate "no suitable HttpMessageConverter"

i realize that something wired goes on with a project i try to create. I'm using the RestTemplate. And i'm trying to connect with a server and retrieve data. All went good until the time i decide to broke my program in more than one controller…
Alex Dowining
  • 980
  • 4
  • 19
  • 41
0
votes
1 answer

rest template serialize/deserialize Map objects with variosu key , value pairs

I am using resttemplate with jackson to marshall/unmarshall java/json objects. What would be the best strategy to serialize/deserialize a Map that may contain key value pairs such that keys are strings and values could be various types for…
devMKZ
  • 39
  • 2
  • 8
0
votes
2 answers

Can't get character encoding to work from android client with resttemplate and json

Ok, so ive tried everything but asking at stackoverflow... I'm trying to perform a REST call with some http params from an Android using httpclient and resttemplate to a server-side Spring controller. All my Swedish chars end up on the server as…
Mathias
  • 3,879
  • 5
  • 36
  • 48
0
votes
1 answer

Spring 3.0 REST service 401 Unauthorized

I'm using RestTemplate from Spring 3.0. When I execute something like this: restTemplate.getForObject(url, Map.class); I got: org.springframework.web.client.RestTemplate- GET request for "http://somewhere.com..." resulted in 401 (Unauthorized);…
Randomize
  • 8,651
  • 18
  • 78
  • 133
0
votes
1 answer

Spring RestTemplate Equivalent

I am using Spring Resttemplate and have an issue about generics: Generics with Spring RESTTemplate I want to look at other equivalent libraries that I can handle that situation. Are there any other choices?
kamaci
  • 72,915
  • 69
  • 228
  • 366