Java

Saturday, 24 November 2012

Restful webservice json,spring dao


Restful webservice json, spring dao:
                                                                                                                           By,G.madhav
                                         

TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 


                                                                               
                                                             
Dept.java



package madhav.pojodao;


public class Dept {
      public int deptno;
      public String dname,loc;
      public int getDeptno() {
            return deptno;
      }
      public void setDeptno(int deptno) {
            this.deptno = deptno;
      }
      public String getDname() {
            return dname;
      }
      public void setDname(String dname) {
            this.dname = dname;
      }
      public String getLoc() {
            return loc;
      }
      public void setLoc(String loc) {
            this.loc = loc;
      }
}



 DeptDao.java


package madhav.daoimpl;
import madhav.pojodao.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.*;
import java.util.*;
public class DeptDao {
            JdbcTemplate jt;
            public DeptDao(JdbcTemplate jt)
            {
            this.jt=jt;
            }
            public List<Dept> selectRecords()
            {
                 String sql="select d.deptno as deptno, d.dname as dname, d.loc as loc from department d"; 
                 System.out.println("select record form Rommapper");
                  DeptRecord dd=new DeptRecord();
                 List<Dept> depts=jt.query(sql, dd);
                 System.out.println(depts.size()+"-------------");
                 return depts;
                 }
            private static final class DeptRecord implements RowMapper<Dept>
            {
                        public Dept mapRow(ResultSet rs, int rownum) throws SQLException {
                                    // TODO Auto-generated method stub
                                    Dept d=new Dept();
                                    d.deptno=rs.getInt("deptno");
                                    d.dname=rs.getString("dname");
                                    d.loc=rs.getString("loc");
                                    return d;
                        }
            }


                              d.deptno=rs.getInt("deptno");
                                    d.dname=rs.getString("dname");
                                    d.loc=rs.getString("loc");

        These above lines of code will provide mapping deatils to the spring.means whech properties of a Dept class will match which attributes of department table. 

List<Dept> depts=jt.query(sql, dd);

Hear  query() method will get the records from the database and return as a list of depts.


DeptResource.java



package madhav.api;                                                                  
import java.util.ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

import madhav.daoimpl.*;
import madhav.pojodao.Dept;

import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.*;
import org.apache.commons.dbcp.BasicDataSource;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

@Path("/dept")
public class DeptResource {
    @GET
    @Produces("application/json")
    public String getClichedMessage()  {
       //spring dao
      
        JSONObject jsonObj = null;
        JSONArray jsArray =null;
        String names=null;
       try
       {
              jsonObj=new JSONObject();
              jsArray = new JSONArray();
               DeptDao deptDao;
             ApplicationContext beanfactory;
             beanfactory=new ClassPathXmlApplicationContext("applicationContext.xml");
             deptDao = (DeptDao)beanfactory.getBean("deptdao");
             System.out.println("extract data");
            List<Dept> depts= deptDao.selectRecords();
           Iterator<Dept> it=depts.iterator();
            while(it.hasNext())
            {
                    Dept d=it.next();
                    jsArray.put(d.deptno+"--" + d.dname +"---" + d.loc);
       }
      
             }
       catch(Exception e)
       {     
       }
              return jsArray.toString();
    }
}


@Produces("application/json")    
 
This will describes after completion of getClichedMessage() 
method execution it will return the content to the webservice caller, 
 of media type what we mension(“application/json”).


ApplicationContext.xml



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

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mysql</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>mysql</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="deptdao" class="madhav.daoimpl.DeptDao">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>

</beans>



Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>  
  <servlet>
       <display-name>JAX-RS REST Servlet</display-name>
       <servlet-name>JAX-RS REST Servlet</servlet-name>
       <servlet-class>
              com.sun.jersey.spi.container.servlet.ServletContainer
       </servlet-class>
       <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
       <servlet-name>JAX-RS REST Servlet</servlet-name>
       <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>




Accessing rest services by using fireforx  rest client:








TUTIORALS FROM MADHAV:

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

    SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 

 

 

 

 

 







1 comment: