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.
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- You need to create two basic java classes (I attached them to the post)
- 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). - 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.
- final class RestApplication extends org.restlet.Application
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>
- Your restlet application context parameter :
- 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:
Notice that
<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>
<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.
Attachments:
RESTlet and Spring
No comments:
Post a Comment