Spring 3 Rest Web Service Integration
Hi Readers,
This is my third post. I am going to start how to build Restful Web Service using Spring 3.1 MVC.
We can use the existing Spring MVC request mapping and the help of JAX-RS 2.0 implementation for JSON and XML marshaling/demarshaling.
JAVA Configuration
We need to change the environmental system variable JAVA_HOME to C:\Java\jdk1.6.
For Ubutnu user install Java apt-get install openjdk-7-jdk
set Java using JAVA_HOME =/usr/lib/jvm/java-7-sun
JBoss 7 Installation
We can download JBoss 7 from below JBoss site
http://www.jboss.org/jbossas/downloads/
Download the Zip file.
SET JBOSS_HOME=C:\jboss-as-7.1.1.Final
C:\jboss-as-7.1.1.Final\bin>service.bat install
For Ubutnu users download JBoss 7 using below command
wget http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.tar.gz
Finally unzip JBoss 7 and put in folder /usr/local/share/JBoss
tar xfvz jboss-as-7.1.1.Final.tar.gz
mv jboss-as-7.1.1.Final /usr/local/share/jboss
MySql Installation
We need to download MySql from the below site
http://dev.mysql.com/downloads/mysql
We can install MySql for Ubutnu users using below command
sudo apt-get install mysql-server
We can create database new database using below command
mysqladmin create <databasename>
Dumping Tables in MySql Database from DB Scripts
First we need to create the database in MySql DB.
eate Create database MyDB;
We can dump the tables in MySql databse from the DB Script using below command
mysql>use SatyaDB;
mysql>Source /usr/local/share/DB_Dump_file.sql;
We will get all the tables from the dump file into my newly created database.
We need to change the environmental system variable JAVA_HOME to C:\Java\jdk1.6.
For Ubutnu user install Java apt-get install openjdk-7-jdk
set Java using JAVA_HOME =/usr/lib/jvm/java-7-sun
JBoss 7 Installation
We can download JBoss 7 from below JBoss site
http://www.jboss.org/jbossas/downloads/
Download the Zip file.
SET JBOSS_HOME=C:\jboss-as-7.1.1.Final
C:\jboss-as-7.1.1.Final\bin>service.bat install
For Ubutnu users download JBoss 7 using below command
wget http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.tar.gz
Finally unzip JBoss 7 and put in folder /usr/local/share/JBoss
tar xfvz jboss-as-7.1.1.Final.tar.gz
mv jboss-as-7.1.1.Final /usr/local/share/jboss
MySql Installation
We need to download MySql from the below site
http://dev.mysql.com/downloads/mysql
We can install MySql for Ubutnu users using below command
sudo apt-get install mysql-server
We can create database new database using below command
mysqladmin create <databasename>
Dumping Tables in MySql Database from DB Scripts
First we need to create the database in MySql DB.
eate Create database MyDB;
We can dump the tables in MySql databse from the DB Script using below command
mysql>use SatyaDB;
mysql>Source /usr/local/share/DB_Dump_file.sql;
We will get all the tables from the dump file into my newly created database.
Spring MVC Context Configuration
This is the entry point to the server. Contains all the configuration and initialization etc...
We need to enable the annotation driven configuration.The base package we need to scan is com.satya.
<mvc:annotation-driven>
<context:component-scan base-package="com.satya"/>
spring-servlet.xml
<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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.satya" />
</beans>
Spring Integration Web Container
org.springframework.web.servlet.DispatcherServlet is the front controller.
The web.xml file will be kept WebContent/WEB-INF directory of your web application.Upon initialization of spring DispatcherServlet, the framework will try to load the application context from a file named [servlet-name]-servlet.xml located in the application's WebContent/WEB-INF directory. In this case our file will be spring-servlet.xml.
The web.xml file will be kept WebContent/WEB-INF directory of your web application.Upon initialization of spring DispatcherServlet, the framework will try to load the application context from a file named [servlet-name]-servlet.xml located in the application's WebContent/WEB-INF directory. In this case our file will be spring-servlet.xml.
web.xml
<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>Spring MVC REST</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/rest</url-pattern>
</servlet-mapping>
</web-app>
If we don't want to go with default file name as [servlet-name]-servlet.xml and default location as WebContent/WEB-INF. We can customize the file name and location by adding the servlet listener..
We need to add ServletLoaderListener in the web.xml file.
web.xml
<web-app...>
<!-------- DispatcherServlet definition goes here----->
....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
If we don't want to go with default file name as [servlet-name]-servlet.xml and default location as WebContent/WEB-INF. We can customize the file name and location by adding the servlet listener..
We need to add ServletLoaderListener in the web.xml file.
web.xml
<web-app...>
<!-------- DispatcherServlet definition goes here----->
....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Service/Controller
We need to write the web service in our Spring MVC controller. Base on our requirement, scale and scope each module contains one or more controller.
@Controller
@RequestMapping(value="/module")
public class SampleController{ }
Controller might holds the request url path. Here the request url path is "/module"
Controller holds the method and rest url path.
@Autowired
ServiceManager serviceManager;
@Autowired
ServiceManager serviceManager;
@RequestMapping(value = "/path" method = RequestMethod.POST)
public ModelAndView getdata(@Valid @RequestBody SampleRequest request,BindingResult result){
if(result.hasErrors()){
}
SampleResponse sampleResponse = serviceManager.getdata(request);
return new ModelAndView("SampleResponse","sampleResponse", sampleResponse);
}
ServiceManager
Calls the DAO methods and holds the business logic.
DataAccessObject
We need to initialize jdbc template in the DAO file.
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
Here datasource is initialize the datasource from the dispatcher servlet.
We can change the datasource later if required if required. We can also specify more than one datasource.
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
We need to implement the DAO method for generating response.
public SampleResponse getdata(SampleRequest request){
String sql="select stdId,stdName from student where stdId=:stdId";
SqlParameterSource source=new BeanPropertySqlParameterSource(request);
BeanPropertyRowMapper<SampleResponse> rowmapper=new BeanPropertyRowMapper(SampleResponse.class);
SampleResponse response=this.jdbcTemplate.queryForObject(sql, source, rowmapper);
return response;
}
Finally the Response POJO will be sent to the Dispatcher Servlet.
SampleRequest
This class is used for marshaling.
package com.satya.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SampleRequest
{
String stdName;
public int getstdName() { return stdName; }
public void setstdName(int stdName) { this.stdName = stdName; }
public void setstdName(int stdName) { this.stdName = stdName; }
}
SampleResponse
This class is used for unmarshalling.
package com.satya.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SampleResponse
{
int stdId;
String stdRegNo;
String stdName;
String stdEmailAddress;
int stdMobileNo;
String stdName;
String stdEmailAddress;
int stdMobileNo;
public int getstdId() { return stdId; }
public void setstdId(int id) { this.stdId = stdId; }
public int getstdRegNo() { return stdRegNo; }
public void setstdRegNo(int stdRegNo) { this.stdRegNo = stdRegNo; }
public int getstdName() { return stdName; }
public void setstdName(int stdName) { this.stdName = stdName; }
public int getstdEmailAddress() { return stdEmailAddress; }
public int getstdRegNo() { return stdRegNo; }
public void setstdRegNo(int stdRegNo) { this.stdRegNo = stdRegNo; }
public int getstdName() { return stdName; }
public void setstdName(int stdName) { this.stdName = stdName; }
public int getstdEmailAddress() { return stdEmailAddress; }
public void setstdEmailAddress(int stdEmailAddress) { this.stdEmailAddress = stdEmailAddress; }
}
Response Preparation
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="rss" value="application/rss+xml" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
<!-- RSS View -->
<bean class="com.mkyong.common.rss.RssFeedView" />
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.satya</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<!-- If no extension matched, use JSP view -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Rest URL
http://localhost:8080/rest/module/path
No comments:
Post a Comment