Monday, September 15th, 2008

Configuring Restlet 1.1 with Spring

By Doug Burns

Back in May I wrote a post about using Spring with Restlet 1.0.x and promised another about doing the same with Restlet 1.1.x, which provides better Spring support. Well, the time has come, so here goes!

In Restlet 1.1, which is currently at Release Candidate 1, you can now configure an application completely within the Spring application context. This means that the only Java classes you will need to create are one for starting the application, and one for each of the resources in your class. Although the Restlet authors prefer specifying the configuration in code, I like specifying it in an external, non-compiled configuration file, and this provides a great way to do that.

Now on to the code! A single-resource application can be configured with only three files: One XML file for defining the Spring beans, one class for defining the resource, and finally, one class for instantiating the Spring context and starting the application.

Spring Application Context

This defines all the Spring beans (Java Object instances) that are needed to run the application. In a non-Spring configured
application these would all be instantiated in your main application class.

  • exampleResource: A standard Resource. In a normal application you would define many resources in the attachments map.
  • restletRouter: Determines how resource URLs map to resource classes.
  • exampleApplication: Associates the router with an application.
  • restletServer: Defines the protocol and HTTP port for the internal server.
  • restletComponent: Associates the server and application. This is the object that will be used to start the server.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean name="exampleResource"
class="com.arc90.example.ExampleResource" scope="prototype" />
<bean name="restletRouter"
class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/example">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="createResource" bean="exampleResource"/>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="exampleApplication"
class="org.restlet.Application">
<property name="root" ref="restletRouter"/>
</bean>
<bean id="restletServer"
class="org.restlet.ext.spring.SpringServer">
<constructor-arg value="http" />
<constructor-arg value="8182" />
</bean>
<bean id="restletComponent"
class="org.restlet.ext.spring.SpringComponent">
<property name="server" ref="restletServer" />
<property name="defaultTarget" ref="exampleApplication"/>
</bean>
</beans>

Resource Class

This is just a simple resource that displays "It worked!" when it is accessed through GET.

package com.arc90.example;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
/**
* Handles requests for the example resource
*/
public class ExampleResource extends Resource
{
public void init(Context context, Request request, Response response)
{
super.init(context, request, response);
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}
public Representation represent(Variant variant) throws ResourceException
{
return new StringRepresentation("It worked!", MediaType.TEXT_PLAIN);
}
}

Application Class

This class contains the main method for starting the application. It instantiates the Spring application context,
where creates instances for the beans defined in application-context.xml. To start the application, the component
bean is retrieved from the application context, then its start() method is called.

package com.arc90.example;
import org.restlet.ext.spring.SpringComponent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Launches the Restlet application
*/
public class ExampleApplication
{
/**
* Get the restlet component from Spring, then starts it
* @param args
*/
public static void main(String[] args)
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] { "application-context.xml" });
try
{
SpringComponent component = (SpringComponent) context.getBean("restletComponent");
component.start();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}

One Response

  1. Rahul said:

    Do you have some sample code with the tomcat server instead of inbuild server from restlet also which can work with Restlet 2.
    Thanks,
    Rahul

Leave a Comment