Wednesday, 6 February 2008

Restlet framework integration with Spring

While I am working in my project (Android/weNear), we decided to expose our server side functionality using web-service interface such that we can easily build web-based consol for weNear beside the Android handset client one.

After our eSpace sessions for the REST architecture, I see that REST architecture is more lighter and flexible in use than SOAP one for web-services (check small comparison Giving SOAP a REST). Just to remember, REST architecture is based on http protocol and commands only. No extra data or handling needed to call your REST web-service (as in SOAP protocol). Just use http connection and enjoy dealing with any data formats you want as supported by the REST server (XML, JSON, etc).

Restlet framework is one from a lot of REST web-service frameworks. It is great and lightweight REST framework for java. The problem is that it lakes of documentation.

The good point is that it has extension for integration with Spring framework (the one we are using in weNear server). Next I will elaborate how you can integrate RESTlet framework with Spring framework.

  1. Add the following libs to your application classpath:
    org.restlet-<VERSION>.jar
    org.restlet.ext.spring-<VERSION>.jar
    com.noelios.restlet-<VERSION>.jar
    com.noelios.restlet.ext.servlet-<VERSION>.jar

  2. You need to create two basic java classes (I attached them to the post)

    1. final class RestApplication extends org.restlet.Application
      This is the main starting point for RESTlet framework to be loaded with your web-application. You will override the method “createRoot()” that starts to load your spring context and initialize your REST manager bean with basic router (refer to RESTlet documentation).

    2. final class RestManager
      This classes will contain your resource mapping. Every map entry will contain resource URL as key and Restlet implementation class as value for this key. This Retlet implementation class is the one who will handle the call to this resource URL.


  3. You need to add some configuration in your web.xml for your RESTlet framework to start.

    • Your restlet application context parameter :

      <context-param>
      <param-name>org.restlet.application</param-name>
      <param-value>
      <RESTLET_APPLICATION_CLASS_PATH>
      </param-value>
      </context-param>

    • Your Restlet servlet and its mapping :

      <servlet>
      <servlet-name>RestServlet</servlet-name>
      <servlet-class>
      com.noelios.restlet.ext.servlet.ServerServlet
      </servlet-class>
      </servlet>

      <servlet-mapping>
      <servlet-name>RestServlet</servlet-name>
      <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>


  4. The final point is creating your Restlet manager bean that will hold your resource/restlet mapping. In your applicationContext.xml you will create the next bean:

    <bean id="manager" class="<RESTLET_MANAGER_CLASS_PATH>">
    <property name="resourceMappings">
    <bean class="java.util.HashMap">
    <constructor-arg>
    <map>
    <entry key="<RESOURCE_URL>">
    <ref local="<RESTLET_BEAN>"/>
    </entry>
    </map>
    </constructor-arg>
    </bean>
    </property>
    </bean>
    Notice that
    <RESOURCE_URL> is your resource URL that will be requested by the client to invoke <RESTLET_BEAN> class, e.g. “/user/1”
    <RESTLET_BEAN> is spring bean for the class that extends org.restlet.Restlet and will handle the call for the given RESOURCE_URL.

Now, you have Restlet framework integrated with Spring. All you need to do for adding more rest resources is creating bean for your restlet implementation and create entry in your RestletManager bean that maps your resource URL to your Restlet implementation bean.

Attachments:
RESTlet and Spring

No comments: