spring+cxf+hibernate  發佈restful WebService服務

項目目錄結構

002TKdqrgy6Vtmnd92db5&690



項目下載路徑: http://pan.baidu.com/s/1o6H06LW   (如果項目路徑實效,可以qq找我,下面有我qq)


1、we b.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

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>SWEATTIME</display-name>

<!-- 加載 spring容器 -->

 <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

    <listener>

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

    </listener>

<!-- 解決中文亂碼問題 -->

 <filter>  

      <filter-name>CharacterEncodingFilter</filter-name>  

      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  

      <init-param>  

          <param-name>encoding</param-name>  

          <param-value>utf-8</param-value>  

      </init-param>  

  </filter>  

  <filter-mapping>  

     <filter-name>CharacterEncodingFilter</filter-name>  

     <url-pattern>/*</url-pattern>  

 </filter-mapping>  


<!-- cxfservlet -->

<servlet>

<servlet-name>cxf</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<!-- 本系統webservice的路徑必須以/ws/開頭 -->

<servlet-mapping>

<servlet-name>cxf</servlet-name>

<url-pattern>/ws/*</url-pattern>

</servlet-mapping>


</web-app>




2、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xmlns:jaxrs="http://cxf.apache.org/jaxrs" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:cache="http://www.springframework.org/schema/cache"

    xsi:schemaLocation="

                    http://www.springframework.org/schema/beans

                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

                    http://www.springframework.org/schema/tx

                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

                    http://www.springframework.org/schema/aop

                    http://www.springframework.org/schema/aop/spring-aop-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/cache

                    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

                    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

                              http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd

            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

 

<!-- 

   引入properties配置文件

    -->

    <context:annotation-config />

    <context:component-scan base-package="cn.xiaoke.ws.cxf.rest" /><!-- 改爲你的包名 -->

 

 <aop:aspectj-autoproxy />

 <tx:annotation-driven />

    

    

   <bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:jdbc.properties</value>

</property>

</bean>

    

   <bean id="dataSource" destroy-method="close"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="${jdbc.driverClassName}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</bean>


<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />  


<!-- hibernate自身屬性 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.format_sql">true</prop>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.hbm2ddl.auto">create</prop>

</props>

</property>

<!-- Hibernate配置文件 -->

<property name="configLocation">

<value>classpath:hibernate.cfg.xml</value>

</property>

    <property name="packagesToScan">

<value>

cn.xiaoke.ws.cxf.rest.pojo*  <!-- 改爲你的包名 -->

</value>

</property>

   

  

</bean>  

<!-- 用於指定持久化實現廠商類 -->

    <!-- 配置HibernateTemplate -->

<bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

<!-- 配置聲明式的的事務管理(基於註解的配置方式) -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

    

<bean id="studentService" class="cn.xiaoke.ws.cxf.rest.service.impl.StudentServiceImpl"/>

<bean id="teacherService" class="cn.xiaoke.ws.cxf.rest.service.impl.TeacherServiceImpl"/>

<!-- 發佈rest服務 

使用jaxws:server和jaxws:endpoint可以發佈服務

webservice地址=tomcat地址+cxf servlet的路徑+/weather

 -->

<jaxrs:server address="/rest">

<jaxrs:serviceBeans>

   <ref bean="studentService"/>

   <ref bean="teacherService"/>

</jaxrs:serviceBeans>

</jaxrs:server>


</beans>



3、hebernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<mapping class="cn.xiaoke.ws.cxf.rest.pojo.Student" />

<mapping class="cn.xiaoke.ws.cxf.rest.pojo.Teacher" />

</session-factory>


</hibernate-configuration>


4、jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc\:mysql\://localhost\:3306/RestWs

jdbc.username=root

jdbc.password=



5、StudentService.java

package cn.xiaoke.ws.cxf.rest.service;


import java.util.List;


import javax.jws.WebService;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;


import cn.xiaoke.ws.cxf.rest.pojo.Student;


/**

 * 

 * @author 柯木超

 * @QQ  751776425

 * 郵箱  [email protected]

 * @version 1.0

 */

@WebService

@Path("/student")

public interface StudentService {

//查詢學生信息

@GET //http的get方法

@Path("/query/{id}")//id參數通過url傳遞

@Produces({"application/json;charset=utf-8", MediaType.APPLICATION_JSON})//設置媒體類型xml格式

public Student queryStudent(@PathParam("id")long id);

//查詢學生列表

@GET //http的get方法

@Path("/querylist/{type}")

@Produces({"application/json;charset=utf-8",MediaType.APPLICATION_JSON})//設置媒體類型xml格式和json格式

//如果想讓rest返回xml需要在rest的url後邊添加?_type=xml,默認爲xml

//如果想讓rest返回json需要在rest的url後邊添加?_type=json

public List<Student> queryStudentList(@PathParam("type") String type);


}

6、StudentServiceImpl.java

package cn.xiaoke.ws.cxf.rest.service.impl;



import java.util.List;


import javax.annotation.Resource;


import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import cn.xiaoke.ws.cxf.rest.dao.StudentDao;

import cn.xiaoke.ws.cxf.rest.pojo.Student;

import cn.xiaoke.ws.cxf.rest.service.StudentService;


@Transactional

@Service("studentService")

public class StudentServiceImpl implements StudentService {


@Resource

private StudentDao studentDao;

@Override

public Student queryStudent(long id) {

return studentDao.queryStudent(id);

}


@Override

public List<Student> queryStudentList(String type) {

return studentDao.queryStudentList(type);

}


}

7、TeacherService.java

package cn.xiaoke.ws.cxf.rest.service;

import java.util.List;

import javax.jws.WebService;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

import cn.xiaoke.ws.cxf.rest.pojo.Teacher;

/**

 * 

 * @author 柯木超

 * @QQ  751776425

 * 郵箱  [email protected]

 * @version 1.0

 *

 */

@WebService

@Path("/teacher")

public interface TeacherService {

//查詢學生信息

@GET //http的get方法

@Path("/query/{id}")//id參數通過url傳遞

@Produces(MediaType.APPLICATION_XML)//設置媒體類型xml格式

public Teacher queryTeacher(@PathParam("id")long id);

//查詢學生列表

@GET //http的get方法

@Path("/querylist/{type}")

@Produces({"application/json;charset=utf-8",MediaType.APPLICATION_ATOM_XML})//設置媒體類型xml格式和json格式

//如果想讓rest返回xml需要在rest的url後邊添加?_type=xml,默認爲xml

//如果想讓rest返回json需要在rest的url後邊添加?_type=json

public List<Teacher> queryTeacherList(@PathParam("type") String type);

}



8、TeacherServiceImpl.java

package cn.xiaoke.ws.cxf.rest.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import cn.xiaoke.ws.cxf.rest.dao.TeacherDao;

import cn.xiaoke.ws.cxf.rest.pojo.Teacher;

import cn.xiaoke.ws.cxf.rest.service.TeacherService;

@Transactional

@Service("teacherService")

public class TeacherServiceImpl implements TeacherService {

@Resource

private TeacherDao teacherDao;

@Override

public Teacher queryTeacher(long id) {

return teacherDao.queryTeacher(id);

}

@Override

public List<Teacher> queryTeacherList(String type) {

return teacherDao.queryTeacherList(type);

}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章