Accessing REST services from Spring 3

June 14th 2010 · Java

1

If you’re using Spring 3 and want to access REST-based services, then you’re in luck because it’s extremely easy.

Let’s say you wanted to pull content from an Alfresco server. A simple way to do that would be to use the direct download URL. But that requires a login ticket. Which you get using a REST call.

In order to generate a login ticket, you call the Alfresco login service with a userid and password:

/alfresco/service/api/login?u=userid&pw=password

Assuming the userid and password are correct, Alfresco will respond back with this simple XML:

<?xml version="1.0" encoding="UTF-8"?>
<ticket>TICKET_18d49dbd5de400c3fa1254b46e46ac51fbd0934e</ticket>

Using Spring it becomes trivial to login and get the ticket:

RestTemplate rest = new RestTemplate();

Map<String, String> params = new HashMap<String, String>();
params.put("user", apiUser);
params.put("password", apiPassword);
Source result = rest.getForObject("http://localhost:8080/service/api/login?u={user}&pw={password}", Source.class, params);

XPathOperations xpath = new Jaxp13XPathTemplate();
return xpath.evaluateAsString("//ticket", result);

This deserves a little explanation. The RestTemplate was introduced in Spring 3 to give REST access the same support as JDBC. It serves as a wrapper to hide all the details in accessing REST resources, including opening and closing connection objects and converting responses to Java objects.

Just like Spring JDBC templates, you can pass a Map of keywords and values. You can also provide parameters positionally, getForObject("url", Source.class, param1, param2, param3, ...).

The Source.class parameter is the return type. The RestTemplate will invoke a HtmlMessageConverter to transform the content returned by the REST call into a Java object.

In the example, Spring will use the pre-registered SourceHttpMessageConverter to treat the stream returned by the REST call as a javax.xml.transform.Source object. You can register custom converters for your own objects with the RestTemplate if you like.

Once you have the Source object, you can operate on it using Spring XML. In this case, we instantiate a concrete implementation of the XPathOperations object, Jaxp13XPathTemplate. XPathOperations is another Spring template object (XPathTemplate). You can use it to map XML into objects. In our case, we use XPath to pull out the contents of the <ticket> tag as a String using a convenience method – xpath.evaluateAsString("//ticket", result).

Once you have the ticket, it’s a simple matter to perform the exact same process using the Alfresco direct download URL:

/alfresco/download/direct?path=xyz&ticket=ticketvalue

If you’re pulling out HTML or text content, you can use the default StringHttpMessageConverter to pull out the file contents directly.

One last wrinkle if you’re implementing this in your own code. The Spring XML library is part of Spring WS, not part of the base Spring package. So when you add it to your Maven POM, it’ll look like this:

<dependency>
    <groupId>org.springframework.ws</groupId>
    <artifactId>spring-xml</artifactId>
    <version>${spring-xml.version}</version>
</dependency>
Author:

Share:

  • email
  • Twitter
  • Facebook
  • Slashdot
  • LinkedIn
  • Digg
  • DZone
  • Reddit