Posted on by & filed under Content - Highlights and Reviews, Web Development.

In my Spring MVC and Rest post, I walked through the creation of a RESTful web service with Spring MVC. Let’s now take a look at how the RestTemplate acts as a client for your RESTful web services.

A RESTful web service needs to be consumed by a client-side component. Spring comes with a class, called RestTemplate, which acts as a client allowing you to emulate all of the HTTP methods and manipulate the HTTP request and response. Let’s look at how you can do a POST request:

The postForLocation method accepts the URI template as its first argument, which is the object to be posted as the second argument. The following arguments list the parameters to be replaced into the template. Alternatively, you can use a different version of the same method accepting a Map holding the parameters to be replaced in the template instead of an argument list, as shown below:

If you want to emulate a GET request, instead, you may want to use the getForObject method:

The second argument defines the class you want to convert the HTTP response into, in this case the String class.

You can also define the RestTemplate class in your Spring application context file, as you can view here:

The restTemplate bean holds the messageConverters property defining one of the implementation of the HttpMessageConverter interface. In this case we are using the MarshallingHttpMessageConverter to convert objects into XML. To marshall and unmarshall, we are using the XStreamMarshaller class defined in the xstreamMarshaller bean.

You should now have a good idea of how to use the RestTemplate as a client for your RESTful web services in Spring MVC.

Safari Books Online has the content you need

Here are some Spring books from Safari Books Online:

Totally revised for Spring 3.0, Spring in Action, Third Edition is a hands-on guide to the Spring Framework. It covers the latest features, tools, and practices including Spring MVC, REST, Security, Web Flow, and more.
The release of Spring Framework 3 has ushered in many improvements and new features. Spring Recipes: A Problem-Solution Approach, Second Edition continues upon the bestselling success of the previous edition but focuses on the latest Spring 3 features for building enterprise Java applications.
Spring Batch in Action is an in-depth guide to writing batch applications using Spring Batch. Written for developers who have basic knowledge of Java and the Spring lightweight container, the book provides both a best-practices approach to writing batch jobs and comprehensive coverage of the Spring Batch framework.
Spring Web Services 2 Cookbook includes a wide variety of recipes that covers most important topics used in real-world applications. It is a well-rounded guide covering a lot of ground in the Spring Web Services domain using systematic arranged chapters and focused recipes.

About this author

Sebastiano Armeli-Battana is software engineer focused on JavaScript and Java development and he is really passionate about Web technologies. He works as consultant in SMS Management & Technology adopting Java technologies and he also works as Web freelancer. He is the author of a jQuery plugin called JAIL and he maintains his personal site at

Tags: RESTf, Spring RestTemplate, XML,

One Response to “Spring MVC Tip: Using The Spring RestTemplate”