Sep 11, 2009
Spring MVC, Restlet integration on Tomcat
In this blog you will learn step-by-step how to build a restful MVC application using Spring MVC and Restlet framework on a Tomcat Server. I assume that you have basic maven knowledge to build your application which’s required. To build application you will need these following dependencies:
<!-- restlet dependencies --> <dependency> <groupId>com.noelios.restlet</groupId> <artifactId>com.noelios.restlet</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>com.noelios.restlet</groupId> <artifactId>com.noelios.restlet.ext.servlet</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>com.noelios.restlet</groupId> <artifactId>com.noelios.restlet.ext.spring</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>org.restlet</groupId> <artifactId>org.restlet.ext.spring</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <!-- spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>2.5.5</version> </dependency>
Here ist the restlet repository entry for your pom.xml :
<repositories> <repository> <id>maven-restlet</id> <name>Public online Restlet repository</name> <url>http://maven.restlet.org</url> </repository>
After you got the needed jars from your repository, you need some required entries for configuring your mvc application, not surprisingly, in your web.xml.
First of all you need to define Spring’s ContextLoaderListener to let the context be initialized.
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
Spring’s DispatcherServlet is a Front Controller. In this case it is responsible for processing all requests directed to *.html pattern. We set here load-on-startup property highly to let the servlet initialize itself on server startup due to performance issues.
<servlet> <servlet-name>entry</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/entry.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>entry</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
Second, you have to configure restlet servlet
<servlet> <servlet-name>rservlet</servlet-name> <servlet-class> com.noelios.restlet.ext.spring.RestletFrameworkServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/rservlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>rservlet</servlet-name> <url-pattern>/restapp/*</url-pattern> </servlet-mapping>
That’s all with web.xml. Now, let’s add our spring configurations into our project, entry.xml and rservlet.xml under WEB-INF directory just like configured above in your web.xml. For our sample application entry.xml includes just one bean; our HelloWorldService:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="helloWorldService" class="com.bagdemir.services.HelloWorldServiceImpl"/> <!-- you are encouraged to add more beans --> </beans>
But, rservlet.xml seems more interesting :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="root" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <entry key="/my/{var}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="createResource" bean="sampleResource" /> </bean> </entry> </map> </property> </bean> <!-- restlet resources --> <bean id="sampleResource" class="com.bagdemir.RestSpring.SampleResource" scope="prototype"> <!-- let spring inject your service --> <property name="helloWorldService" ref="helloWorldService"/> </bean> </beans>
SpringRouter is the class which’s responsible for routing the requests to the mapped resources. In this case it routes the requests on “/my/{var}” to “sampleResource” resource, on which the request will be handled. And here is our Resource :
public class SampleResource extends Resource { private HelloWorldService helloWorldService; @Override public void init(Context context,Request request,Response response) { super.init(context, request, response); getVariants().add(new Variant(MediaType.TEXT_PLAIN)); } @Override public boolean allowPost() { return false; } @Override public boolean allowGet() { return true; } @Override public Representation represent(Variant variant) { String message = getHelloWorldService().sayHelloWorld(); return new StringRepresentation(message, MediaType.TEXT_PLAIN); } public HelloWorldService getHelloWorldService() { return helloWorldService; } public void setHelloWorldService(HelloWorldService helloWorldService) { this.helloWorldService = helloWorldService; } }
HelloWorldService will be injected by spring using dependency injection. It has just one method sayHello() which returns “Hello World” string.
That’s all. If you run the application on Tomcat, no extra configuration needed, you will see that message worldwide known “Hello World”.
http://localhost:8080/restapp/my/app

Why does everything have to be so painful in Java web programming?
All that XML authoring made me dizzy.
Actually not. you don’t have to use “maven” build tool in your project. You could still compileyour applications using old methods like “javac” on your console. But it means then that you must include all required components into your project manually. And that way could be more painful if you work in a large project with hundreds of components. (and hundreds of versions of these components). But maven make the life easier. You can create your project files, integrate them with your version control system and upload to your repository. Who wants to use your component, must just declare your project’s artifact, groupid and version in his project file as dependency.
Spring implements MVC and Front controller pattern in a clean way. I can say, all of these xmls above are for seperating concerns and reducing coupling between them , using meta data. Your classes could include all of these logic in their methods like applications implemented with some popular scripting languages. But when things get more complicated, you need more sophisticated solutions than scripting.
Can you please give the tomcat version and jdk version you’re using. This setup is not working with jdk 1.6 and tomcat 6.0
JDK 1.6 and and the application was tested on tomcat 6. Would you like to describe your Problem a little more? have you got some Exceptions ?
Thanks, no exceptions, just a HTTP Status 404. Will check my set up again.
Thank you, it worked, I had some issues due to sitemesh filter configuration.
Hi everything deployed in tomcat 6.0.24 but i encounter also an HTTP 404 error on http://localhost:8080/restapp/my/app
Did you add logging support in your application, for example using log4j ? Could you tell me what your catalina.out and log files say?