-

Wednesday, 14 August 2013

Spring frameworks provides the Restful web services implementation, here is one simple example to implement Web services using Spring.

Software Used

Java 1.7

Spring 3.2.1

Tomcat 7

Eclipse Juno

1. Create a Web project

Create a dynamic web project in eclipse and add spring jars in to web-inf lib folder

image

2. Create a Controller class to implement Web service methods

In this example web service methods return State name upon passing state code from web service call

package com.pretech;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/state")
public class StateController {
	@RequestMapping(value = "/{code}", method = RequestMethod.GET)
	public  @ResponseBody String getState(@PathVariable String code) {
		String result;
		if (code.equals("KL")) {
			result = "Kerala";
		} else {
			result = "Default State";
		}
		return result;
	}
}

3. Create (Spring configuration)dispatcher-servlet.xml to web-inf directory

<?xml version="1.0" encoding="UTF-8"?>
<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"
	xmlns:p="http://www.springframework.org/schema/p"
	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="com.pretech" />
	<mvc:annotation-driven />
  </beans>

4 Update Spring DispatcherServlet details into web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringRestFulExample</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

5 Deploy application on Tomcat Server


Right click on the project and select Run as Tomcat Server. Once it is deployed hit below url to call web service

http://localhost:8080/SpringRestFulExample/state/KL

6. Output


(Used Mozilla RestClient to test web service)



image


7. Download Source code


Spring Restful Example

0 comments: