With Spring 3 querying a REST-based service it’s easy. For first, you need to authenticate on Alfresco repository using an Alfresco Ticket instead of an explicit user name and password. A ticket represents a pre-authenticated user who has already performed the login process. To log in, the url is:


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

<!--?xml version="1.0" encoding="UTF-8"?-->

Using Spring and Java you should write something like that:

RestTemplate restTemplate = new RestTemplate();
Map params = new HashMap();
params.put("user", login);
params.put("password", password);
String url = "http://localhost:8081/alfresco/service/api/login?u={user}&amp;pw={password}";
Source result = restTemplate.getForObject(url, Source.class, params);
XPathOperations xpath = new Jaxp13XPathTemplate();			
this.authenticationTicket = xpath.evaluateAsString("//ticket", result);

The RestTemplate was introduced in Spring 3 to give REST access the same support as JDBC, passing a Map of keywords and values. 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. Source class is the return type.

Once authenticated, we are ready to call Web script to query Alfresco repository:


This web script returns an XML like that:


The above response needs some configuration to marshall objects to XML. All we need to do is define it in our application context and, for that to work fine, you need to tell the marshaller which annotated classes you have:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
            <bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <property name="marshaller" ref="marshaller" />
                <property name="unmarshaller" ref="marshaller" />
<bean name="marshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
    <property name="autodetectAnnotations" value="true"/>
    <property name="annotatedClasses">

At this point we can simply instantiate a RestTemplate object by calling the getBean method on the application context. This is the Java code to do that:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("alfresco-context.xml");
RestTemplate restTemplate = applicationContext.getBean("restTemplate", RestTemplate.class);	
Map<String, String> params = new HashMap<String, String>();
params.put("ticket", this.authenticationTicket);	
params.put("keyword", key);	
String url = "http://localhost:8081/alfresco/service/kipcast/search/brand.xml?q={keyword}&alf_ticket={ticket}";
Brands brandList = (Brands) restTemplate.getForObject(url, Brands.class, params);

Last thing is modify the two beans Brands and Brand putting the proper annotations to both classes:

public class Brands {
	List<Brand> brandList = new ArrayList<Brand>(); 
    public List<Brand> getBrands() {
        return brandList;
	public void setBrands(List<Brand> brandList) {
	    this.brandList = brandList;
	public void addBrands(Brand brand) {
public class Brand {
	private String nodeRef;
	private String name;
	private String relatedCommonName;
	private String supplierName;
        // Getter and setter methods

For more details please take a look to thekspace.com, tedwise.com and springsource.com.

Query Alfresco Web script using REST
Tagged on:         

Leave a Reply