{ by david linsin }

November 30, 2009

Playing with Spring's RestTemplate

A couple of week ago, I saw a talk on Spring 3.0's MVC REST support. It's quite impressive how simple SpringSource made REST development by applying the proven concepts of the core Spring Framework.

One of those great core concepts is Templates. Spring's Data Access Framework is an example, where templates are used extensively. One of the templates is the JDBCTemplate. It provides a set of predefined methods to access a database, without worrying about Connection management or Exception handling. Templates are a nice way to help you the the job done, without constraining flexibility. The book "Building Spring 2 Enterprise Applications", which I reviewed last year, there's a whole chapter on Data Access and the corresponding Templates.

The RestTemplate is the latest child in the family. Arjen Poutsma from SpringSource wrote a blog entry about it and describes it as follows:

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.

In the before mentioned blog post, Arjen shows how to pull pictures from Flickr, using their REST API, and display them in a JFrame. He uses an XML based approach, whereas I'll show you some code, which handles JSON:

You can see it's quite easy to work with a RestTemplate. You simply create an instance, tell that instance what your expected content is going to be and you are good to go. In this case we expect the request and response to be JSON. Spring is using the Jackson JSON Processor, to automagically map POJOs to JSON and all the way back.

The concept of a MessageConverter is really neat. You can set multiple converters and specify to use a certain converter for certain POJOs. You can also handle multiple content types by setting multiple instances of MessageConverter.

In order to make a REST call with the predefined MessageConverter, you simply use the getForObject method. You pass the url to call, the type the response is supposed to be mapped to and the values to be replaced in the url. The type of the response is actually a wrapper class:

It is necessary since the following JSON response, which is represented in the POJO Issue, needs some kind of holder object:

As you can see, it's really easy to work with Spring's new RestTemplate. These couple of lines are enough to retrieve a JSON or XML response from a REST call. With the Template approach, you have the full flexibility to hook almost every method and tweak it to your needs.


salman said...

Just like your JSON object starts with the issue label, which is the name of your object perhaps.
{"issue":{"actual attributes here"}}

mine is rendered into JSON as:

Now when the response comes back from the service the mapping jackson converter throws an exception, saying something like "user" attribute is unknown - not marked as ignorable. Would you know how to avoid this?


David Linsin said...

As you can see in the code sample, I'm not working with my Issue class directly. I had to introduce a mapping class called IssueResponse, which in turn has an attribute called "issue".

salman said...

Thanks for the response David.

Yes, I noticed that bit. But was wondering if there is another way to ignore the first attribute by passing some configuration to the converters. Didn't feel like introducing a wrapper class for the response.

Currently I am using the post and get for Entity, just to get things rolling. Not fully convinced with this approach as well :)

David Linsin said...

Sorry, that was the fastest approach I saw back then. I don't have another solution.

Anonymous said...

Ignore Tags: I saw several @... Annotations in org.springframework.social.greenhouse.Event.java

Here is a part of it

@JsonIgnoreProperties(ignoreUnknown = true)
public class Event {

private long id;

private String title;


  • mail(dlinsin@gmail.com)
  • jabber(dlinsin@gmail.com)
  • skype(dlinsin)