Spring 3 provides support for RESTful Web services through its MVC framework.
The most popular approach for implementing RESTful Web services is Sun's JAX-RS specification. There are several projects available that support JAX-RS such as CXF, Jersey, RESTEasy and Restlet. Most of them provide Spring support also. Spring does not directly support JAX-RS, instead RESTful functionality is added to feature Spring MVC itself.
Spring MVC stands for Model View Controller. It helps in building flexible and loosely coupled web applications. The Model – View – Controller design pattern insures separation of concerns (business logic, presentation logic and navigation logic) in a multi-tier web application. "Controllers" are responsible for receiving the request from the user and calling the back – end services. Models are responsible for encapsulating the application data. Views render response back to the user with using the model object. In short :
When a request is sent to the Spring MVC Framework the following sequence of events happen.
*** The “DispatcherServlet” first receives the request
*** The “DispatcherServlet” consults the “HandlerMapping” and invokes the "Controller" associated with the request
*** The "Controller" process the request by calling the appropriate service methods and returns a “ModeAndView” object to the “DispatcherServlet”. The “ModeAndView” object contains the model data and the view name
*** The “DispatcherServlet” sends the view name to a “ViewResolver” to find the actual “View” to invoke
*** The “DispatcherServlet” passes the model object to the “View” to render the result
*** The “View” with the help of the model data renders the result and return it back to the user
Enough talk! Lets get our hands dirty:
Java Code Geeks : Spring 3 RESTful Web Services
In this tutorial we will show you how to implement a RESTful Web service in Spring or, if you want, to expose an existing Spring service as a RESTful Web service. CRUD (Create - Retrieve - Update - Delete) functionality will be exposed over a RESTful interface providing XML or JSON representation of domain data depending on client preferences! All with minimal coding and configuration.