项目目录结构
项目下载路径: 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>
<!-- cxf的servlet -->
<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
* 邮箱 751776425@qq.com
* @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
* 邮箱 751776425@qq.com
* @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);
}
}
本文出自 “java” 博客,请务必保留此出处http://5378610.blog.51cto.com/5368610/1695283
spring+cxf+hibernate 发布restful WebService服务
原文地址:http://5378610.blog.51cto.com/5368610/1695283