Hi,

I'm a bit late to this party, but I've been playing around with another way of integrating Restlets into an existing Spring-based web application. It uses ServletConverter in a subclass Spring's FrameworkServlet class so that configuring the Restlet servlet is just like configuring a Spring-MVC servlet. It's still experimental, but it seems to be working all right so far.

The servlet is here:

https://svn.bioinformatics.northwestern.edu/studycalendar/trunk/src/main/java/edu/northwestern/bioinformatics/studycalendar/restlets/RestletSpringServlet.java

You add it to your web.xml like so:

    <servlet>
        <servlet-name>restful-api</servlet-name>
<servlet- class > edu .northwestern .bioinformatics.studycalendar.restlets.RestletSpringServlet</servlet- class>
        <init-param>
            <param-name>targetRestletBeanName</param-name>
            <param-value>router</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>restful-api</servlet-name>
        <url-pattern>/api/v1/*</url-pattern>
    </servlet-mapping>

And then configure it in a file named WEB-INF/<servlet-name>- servlet.xml. For this example, that would be WEB-INF/restful-api- servlet.xml . The "targetRestletBeanName" points to a bean in that file which the ServletConverter will delegate to (e.g., a Router).

I've also implemented an alternate Router (BeanNameRouter) which works like Spring-MVC's BeanNameUrlHandlerMapping. With it, you can have a restful-api-servlet.xml doc like this:

<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 name="router" class = "edu .northwestern.bioinformatics.studycalendar.restlets.BeanNameRouter"/>

    <bean name="/studies"
          id="studiesResource" autowire="byName" scope="prototype"
class = "edu.northwestern.bioinformatics.studycalendar.restlets.StudiesResource"
          />

    <bean name="/studies/{study-identifier}/template"
          id="templateResource" autowire="byName" scope="prototype"
class = "edu .northwestern.bioinformatics.studycalendar.restlets.TemplateResource"
          />

</beans>

This file defines two resources -- /studies and /studies/{study- identifier}/template . The BeanNameRouter takes care of creating appropriate Finders for each resource and attaching them to the router via Spring's BeanFactoryPostProcessor mechanism. BeanNameRouter is here:

https://svn.bioinformatics.northwestern.edu/studycalendar/trunk/src/main/java/edu/northwestern/bioinformatics/studycalendar/restlets/BeanNameRouter.java

It depends on SpringBeanFinder:

https://svn.bioinformatics.northwestern.edu/studycalendar/trunk/src/main/java/edu/northwestern/bioinformatics/studycalendar/restlets/SpringBeanFinder.java

SpringBeanFinder is a Finder that resolves Resources out of a BeanFactory by name (an alternative to the cglib-based lookup-method approach suggested with SpringFinder).

Caveat coder: I've only tested this with 1.1-M1, and I think some of the spring integration these classes depend on is only available in that version. I'd be happy to contribute any or all of this code to the Restlet spring extension if it would be useful. (And I've even got unit tests for it.)

Rhett

On Jan 24, 2008, at 9:11 AM, Barry wrote:

Hi all,

I'm quite new to this so let me know if I'm missing something obvious.

I'm trying to create a Spring configured Restlet Application that will run
in a servlet container.

As far as I can see I don't think this is supported (as the ServerServlet
creates the application). Is there a way to do this?

If not I may create a SpringServerServlet that looks the Application that
willget the application from the spring config.

Or alternatively create an an application that proxies its calls to a
application looked up from the spring context.

Which sounds better (or are they both horrible)?

Barry




Reply via email to