Alex Staveley is a software professional passionate about software engineering and technical architecture. He blogs about architectural approaches, Java topics, web solutions and various technical bits and pieces. Alex is a DZone MVB and is not an employee of DZone and has posted 48 posts at DZone. You can read more from them at their website. View Full User Profile

REST Spring 3.0 - Simple Introduction

  • submit to reddit
This post is going to be a very simple introduction to the RestTemplate API introduced in Spring 3.0.
Dogs know how to REST!
The RestTemplate is similar to other Spring templates such as JmsTemplate and JdbcTemplate in that Spring eliminates a lot of boot strap code and thus makes your code much cleaner.  When applications use the RestTemplate they do not need to worry about HTTP connections, that is all encapsulated by the template. They also get a range of APIs from the RestTemplate which correspond to the well know HTTP methods (GET, PUT, POST, DELETE, HEAD, OPTIONS).  These APIs are overloaded to cater for things  like different ways of passing parameters to the actual REST API.  Ok, so let's look a very simple example.  Suppose we want to invoke a well know Twitter REST API to check the Twitter timeline for the user "dublintech".  This API would takes form:

Fire it into your web browser and you'll see you get back the timeline for dublintech in XML.  But you don't want to fire it into your browser, you want to invoke the API in a Java program!  Let's do that.

import org.springframework.web.client.RestTemplate;
public class Spring3RestTest {
    public static void main(String args[]) {
        Spring3RestTest test = new Spring3RestTest();
    private void getTimeline() {
        RestTemplate restTemplate = new RestTemplate();
        String response =
                restTemplate.getForObject("", String.class, new Object[]{});
        System.out.println("Response is=" + response);

That's it. Isn't that incredibly simple. No HTTP Connection handling to worry about. Import one Spring 3.0 class, use it and that's it!  Yes, this post was a very simple introduction. In the real world, if you were using this API you'd obviously be doing something a little more sophisticated for example using XPath to get specific information out of the XML returned. But we don't need to cover that.  The point of this post was really to point how easy it is to use some of the REST 3.0 Spring features. In future posts we'll be looking at more advanced features.

 For now - keep it simple!
3. Twitter REST API
4. Roy Fielding's original REST Paper:



Published at DZone with permission of Alex Staveley, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Cafebabe Ru(ssia) replied on Mon, 2011/12/12 - 6:59am

It seems Spring is reinventing the wheel, RESTEasy Client does this in a better way.

Charlie Mordant replied on Mon, 2011/12/12 - 7:41am

RestEasy is about as old as Spring Rest. Apache CXF also has a REST client API:

Cafebabe Ru(ssia) replied on Mon, 2011/12/12 - 7:59am in response to: Charlie Mordant

> RestEasy is about as old as Spring Rest.


January 21, 2009. RESTEasy 1.0.0.GA released and certified

 Spring 3 was out about a year later

Sandeep Bhandari replied on Mon, 2011/12/12 - 11:44am

Spring is definitely a good option for writing REST web services but only when you are going to use other modules like IoC, MVC or AOP (mainly IoC or MVC). If not utilizing other features then better use some other specialized tool for REST. CXF is a good option. Steps to Write RESTful Web Services CXF Books List RESTful Interview Questions using Java

John David replied on Tue, 2011/12/20 - 11:27am

I think Spring is not reinventing the wheel. It is providing support for REST web services.

Spring has already lot to offer and its integration with web services is a new milestone.

Carla Brian replied on Sat, 2012/04/28 - 10:58pm

RestTemplate can be backed by Commons HttpClient using ClientHttpRequestFactory. Using Commons HttpClient as the backend implementation supports basic authentication in the requests. - Dr Marla Ahlgrimm

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.