Monday, July 5, 2010

Spring 3: RESTful Web service Support 1

So in between some really long days at work, the usual before release panic and regression defects. I finally completed a simple RESTFul example, before getting into the details, here are a couple facts about RESTFul services.

  • REST stands for Representational State Transfer.
  • Each unique URL is a representation of some object.
  • You can get the contents of that object using an HTTP GET.
  • You may use a POST, PUT, or DELETE to modify the object.
  • It is not necessary to use XML as a data format, so for small amounts of data it can be significantly lighter on bandwidth.
  • REST as a protocol does not define any form of message envelope.
  • REST takes advantage of HTTP caches. (A cache can't to do anything a POST; but they can cache GETs and expire those entries based on PUTs and DELETEs.).
  • The Sun specification for RESTFul webservices can be found atJAX-RS
  • Spring does not directly support or implement the JAX-RS spec but instead RESTful functionality is added to feature Spring MVC.

As with my other examples all the source code, config and POM files are available in SVN

As we are exposing information via HTTP this is going to be a web project. I won't be going into details of compiling and deploying a web application, but will go through all the required parts
Starting with the web.xml:

02 <display-name>SpringRestfulExampledisplay-name>
03
04 <context-param>
05 <param-name>log4jConfigLocationparam-name><param-value>/WEB-INF/classes/log4j.propertiesparam-value>context-param>
06
07 <servlet>
08 <servlet-name>Spring3RESTFulservlet-name>
09 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
10 <load-on-startup>1load-on-startup>
11 servlet>
12
13 <servlet-mapping>
14 <servlet-name>Spring3RESTFulservlet-name>
15 <url-pattern>/app/*url-pattern>
16 servlet-mapping>
17
18 <welcome-file-list>
19 <welcome-file>index.jspwelcome-file>
20 welcome-file-list>
21web-app>


The DispatcherServlet will receive the request it will then, based on the servlet mapping
go looking for "Spring3RESTFul-servlet.xml" which in turn defines the controller and view (which are dynamically searched for and created):

07
08 <context:component-scan base-package="javaitzen.spring.rest.controller">
09
10 <beanclass="org.springframework.web.servlet.view.BeanNameViewResolver">
11
12 <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
13 <property name="messageConverters">
14 <list>
15 <ref bean="marshallingHttpMessageConverter">
16 ref>list>
17 property>
18 bean>
19
20 <beanclass="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"id="marshallingHttpMessageConverter">
21 <property name="marshaller" ref="jaxb">
22 <property name="unmarshaller" ref="jaxb">
23 property>property>bean>
24
25 <oxm:jaxb2-marshaller id="jaxb">
26 <oxm:class-to-be-bound name="javaitzen.spring.rest.User">
27 oxm:class-to-be-bound>oxm:jaxb2-marshaller>
28bean>context:component-scan>beans>
The component-scan in the application context above will be used to automatically pick up our @Controller. If you go to /app/users/Bob, the getUser method is executed and their respective name is passed through as a parameter. The Spring JaxB 2 marshaller will be used to marshall the User object in and out of XML. Any of the OXM marshallers can be used, I just simply prefer JaxB.

No comments:

Post a Comment