- 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:
01 | < web-app id = "WebApp_ID" version = "2.4" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/j2ee" xsi:schemalocation = "http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > |
02 | < display-name >SpringRestfulExample display-name > |
03 | |
04 | < context-param > |
05 | < param-name >log4jConfigLocation param-name >< param-value >/WEB-INF/classes/log4j.properties param-value > context-param > |
06 | |
07 | < servlet > |
08 | < servlet-name >Spring3RESTFul servlet-name > |
09 | < servlet-class >org.springframework.web.servlet.DispatcherServlet servlet-class > |
10 | < load-on-startup >1 load-on-startup > |
11 |
servlet > |
12 | |
13 | < servlet-mapping > |
14 | < servlet-name >Spring3RESTFul servlet-name > |
15 | < url-pattern >/app/* url-pattern > |
16 |
servlet-mapping > |
17 | |
18 | < welcome-file-list > |
19 | < welcome-file >index.jsp welcome-file > |
20 |
welcome-file-list > |
21 |
web-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):
01 | < beans xmlns:context = "http://www.springframework.org/schema/context" xmlns:oxm = "http://www.springframework.org/schema/oxm" xmlns:p = "http://www.springframework.org/schema/p" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans |
07 | |
08 | < context:component-scan base-package = "javaitzen.spring.rest.controller" > |
09 |
10 | < bean class = "org.springframework.web.servlet.view.BeanNameViewResolver" > |
11 |
12 | < bean class = "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 | < bean class = "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 > |
28 |
bean > context:component-scan > beans > |
No comments:
Post a Comment