Tuesday, 30 September 2014

Spring 3 Rest Web Service Batch Update with JQuery & Ajax Integration

                 Spring 3  Rest Web Service Batch Update with JQuery & Ajax Integration

In this blog we will see how to configure the restful web services.

DATABASE :

Here I am creating tbl_area table in MySQL area database.

CREATE TABLE tbl_area(
   AREA_ID   INT NOT NULL AUTO_INCREMENT,
   AREA_LOCATION VARCHAR(20) NOT NULL,
   AREA_LAND_MARK  VARCHAR(20) NOT NULL,
   EMAIL  VARCHAR(20) NOT NULL,
   PRIMARY KEY (AREA_ID)

);

Spring Context Configuration :

Spring Context file Contain all the configuration and initialization for Spring MVC Application.
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-Context.xml

cntext2

The ApplicationContext is a subclass of the BeanFactory interface so it has all the functionality a BeanFactory has and more. 

BeanFactory is recommended for lightweight framework like — Mobile and Applet.

1. BeanFactory is core component of Spring IoC container.

2. As ApplicationContext extends BeanFactory so ApplicationContext is more powerful than BeanFactory (like AOP, Internationalization etc.)

You can configure your beans in the Spring IoC container through XML files, properties files, annotations, or even APIs.
1. Container reads these beans by using BeanFactory (or ApplicationContext)
2. Suppose define the beans in XML file, say applicationcontext.xml.
3. Now BeanFactory has to read applicationcontext.xml to load the beans in container.
4. For this BeanFactory needs an interface that can provide the xml file to BeanFactory.
5. This interface called resource object.

<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" />

    <!-- 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/area"/>
      <property name="username" value="root"/>
      <property name="password" value="root"/>
   </bean>

   <!-- Definition for studentJDBCTemplate bean -->
   <bean id="areaJDBCTemplate" 
      class="com.tutorialspoint.AreaJDBCTemplate">
      <property name="dataSource"  ref="dataSource" />    
   </bean>

</beans>

We need to create Spring-Beans.xml file to define the ContentNegotiableViewResolver. The CNVR does not resolve the views itself. It delegates to other view resolver.

Spring-Beans.xml :

Here in Spring-Beans.xml file I am using CNVR to resolve views.

<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">
   
        <bean id="contentNegotiatingViewResolver"
        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="request" value="text/html" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean
                    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                </bean>
                <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.domain.Area</value>
                                </list>
                            </property>
                        </bean>
                    </constructor-arg>
                </bean>
            </list>
        </property>
    </bean>  
</beans>

web.xml :

We need to do some modification to the web.xml file depending upon our Spring Restful web service. I am adding spring beans to init and context param.

1. init param - Spring-Beans.xml
2. context param - Spring-Context.xml

<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/Spring-Beans.xml
  </param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.request</url-pattern>
  <url-pattern>*.json</url-pattern>
  <url-pattern>*.xml</url-pattern>
</servlet-mapping>

<context-param>
        <param-name>ContextConfigurationLocation</param-name>
       <param-value>Spring-Context.xml</param-value>
</context-param>

ServletContextListener Spring provides an implementation class ContextLoaderListener class can be used as a listener, it will be created automatically find WEB-INF / applicationContext.xrnl file. Therefore, if only one configuration file, and the file named applicationContext.xml, you only need to add the following code to the web.xml file: 

<listener> 
<listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class> 
</ Listener>


Next blog I will provide service implementation.....................................

No comments:

Post a Comment

Spring 3 Rest Web Service Batch Update with JQuery & Ajax Integration

                      Spring 3  Rest Web Service Batch Update with JQuery & Ajax Integration Hi     In this blog I will describe the...