Wednesday 31 October 2012

Rest WebService Example Using Spring


Spring MVC framework support for RESTful Web services. This feature is available in Spring 3.
The highlighted approach for implementing RESTful Web services is Sun's JAX-RS specification.
Many tools are available that support JAX-RS such as RESTEasy, Jersey,  CXF and Restlet. Most of them are also providing Spring support. Spring does not directly support JAX-RS. RESTful feature is added to spring MVC itself.

Following series of events are happening when a request is sent to the Spring MVC Framework When a
  • At First the DispatcherServlet receives the request.
  • The DispatcherServlet interacts with the HandlerMapping and invokes the Controller associated with the request.
  • By calling the appropriate service methods the Controller process the request  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.
Example - Rest web service using Spring

STEPS

1) Create a web project in Eclipse.
2) Name it as WelcomeWithSpring.
3) Create a package in src folder and name as spring.example.controller.
4) Create a class named as WelcomeController.java
5) Use the below code in WelcomeController.java
  
package spring.example.controller;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping
("/welcome")
public class WelcomeController {
@RequestMapping(value="/{username}", method = RequestMethod.GET)
public String getUserName(@PathVariable String username, ModelMap model) {
model.addAttribute(
"welcomeValue", username);
return "welcome";
     }
}

6) Modify the web.xml file as below

<web-app id="WebApp_ID" version="2.4"

xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Welcome Using Spring</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app> 
7) Create an xml file and named it as mvc-dispatcher-servlet.xml.Use the below code :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
<context:component-scan base-package="spring.example.controller" /> 
<mvc:annotation-driven /> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

8) Create a folder named pages inside WEB-INF folder 
9) Create a jsp file and name it as welcome.jsp. Use the below code.

<html>
<body>
<h1>This is the Example of Spring REST web service Calling getUserName Method</h1>
<h3>User Name : ${welcomeValue}</h3>
</body>
</html>

10) Use the below jars as library in order to compile and execute this project.



11) Select this project, right click on it, go to option RUN as, select RUN ON SERVER option.

12) Configure Tomcat 6.0 or Tomcat 7.0 version here and add this project. Authomatically server will start after configuring server, adding the project and clicking on the finish option.

13) After hitting the below URL, you will get the output as attached below .
 http://localhost:<port number>/WelcomeWithSpring/welcome/gaurav


 14) Final Project Architecture in Eclipse as below:




No comments:

Post a Comment