标签:
开发环境:maven3.3.3、apache-tomcat-6.0.44、jdk1.6.0_13
开发技术:spring3.2.8、cxf2.7.12、mybatis3.2.8、druid1.0.11
项目截图:
项目源码:
一、配置文件
1、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.neusoft.icelake</groupId> <artifactId>restservice</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>restservice</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.6</java.version> <spring.version>3.2.8.RELEASE</spring.version> <servlet-api.version>2.5</servlet-api.version> <slf4j.version>1.7.5</slf4j.version> <cxf.version>2.7.12</cxf.version> <jackson.version>2.1.4</jackson.version> <mybatis.version>3.2.8</mybatis.version> <mybatisspring.version>1.2.2</mybatisspring.version> <druid.version>1.0.11</druid.version> </properties> <dependencies> <!-- spring begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> <!-- @Inject begin --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- @Inject end --> <!-- servlet begin --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> </dependency> <!-- servlet end --> <!-- cxf begin --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <!-- cxf end --> <!-- JSON begin --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.0</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.0</version> </dependency> <!-- JSON end --> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatisspring.version}</version> </dependency> <!-- AOP begin --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <!-- AOP end --> <!-- jdbc driver --> <dependency> <groupId>localrep.sybase</groupId> <artifactId>jconn</artifactId> <version>2.0</version> </dependency> <!-- connection pool --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> </dependencies> <build> <plugins> <!-- compiler插件, 设定JDK版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>${java-version}</source> <target>${java-version}</target> </configuration> </plugin> </plugins> </build> <!-- PROGRAMER INFO --> <organization> <name>Neusoft</name> <url>http://www.neusoft.com</url> </organization> </project>
2、spring配置文件applicationContext-server.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <context:component-scan base-package="com.neusoft.icelake" /> <import resource="classpath*:META-INF/cxf/cxf.xml" /> <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" /> <jaxrs:server id="restServiceContainer" address="/rest"> <jaxrs:providers> <ref bean="jacksonProvider" /> </jaxrs:providers> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:serviceBeans> <ref bean="wholeQuantityService" /> </jaxrs:serviceBeans> <jaxrs:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxrs:inInterceptors> <jaxrs:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxrs:outInterceptors> </jaxrs:server> <bean id="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"></bean> <bean id="jacksonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"> <property name="mapper" ref="jacksonObjectMapper" /> </bean> </beans>
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" /> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="initialSize" value="0" /> <property name="maxActive" value="20" /> <property name="minIdle" value="0" /> <property name="maxWait" value="60000" /> <property name="validationQuery" value="${jdbc.testSql}" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <property name="minEvictableIdleTimeMillis" value="25200000" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="1800" /> <property name="logAbandoned" value="true" /> <property name="filters" value="mergeStat" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/neusoft/icelake/mappers/**/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.neusoft.icelake" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.neusoft.icelake..*impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> </beans>
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>restservice</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-context*.xml</param-value> </context-param> <listener> <description>spring listener</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
1、entity类(Org.java、Orgs.java)
package com.neusoft.icelake.modules.standard.entity; import java.io.Serializable; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "SapOrg") public class Org implements Serializable { private static final long serialVersionUID = 7446548875479453298L; private String parent_id; private String parent_desc; private String child_id; private String child_desc; private String child_short; private String org_sort; public String getParent_id() { return parent_id; } public void setParent_id(String parent_id) { this.parent_id = parent_id; } public String getParent_desc() { return parent_desc; } public void setParent_desc(String parent_desc) { this.parent_desc = parent_desc; } public String getChild_id() { return child_id; } public void setChild_id(String child_id) { this.child_id = child_id; } public String getChild_desc() { return child_desc; } public void setChild_desc(String child_desc) { this.child_desc = child_desc; } public String getChild_short() { return child_short; } public void setChild_short(String child_short) { this.child_short = child_short; } public String getOrg_sort() { return org_sort; } public void setOrg_sort(String org_sort) { this.org_sort = org_sort; } @Override public String toString() { return "Org [parent_id=" + parent_id + ", parent_desc=" + parent_desc + ", child_id=" + child_id + ", child_desc=" + child_desc + ", child_short=" + child_short + ", org_sort=" + org_sort + "]"; } }
package com.neusoft.icelake.modules.standard.entity; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "SapOrgs") public class Orgs { private List<Org> list; private Org[] array; private Map<String, Org> map; public List<Org> getList() { return list; } public void setList(List<Org> list) { this.list = list; } public Org[] getArray() { return array; } public void setArray(Org[] array) { this.array = array; } public Map<String, Org> getMap() { return map; } public void setMap(Map<String, Org> map) { this.map = map; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.neusoft.icelake.modules.standard.dao.IWholeQuantityDao"> <resultMap id="EmpResultMap" type="com.neusoft.icelake.modules.standard.entity.Emp"> <id column="accno" property="accno" jdbcType="VARCHAR" /> <result column="truename" property="truename" jdbcType="VARCHAR" /> <result column="gender" property="gender" jdbcType="VARCHAR" /> <result column="office_mail" property="office_mail" jdbcType="VARCHAR" /> <result column="title" property="title" jdbcType="VARCHAR" /> <result column="dp_id" property="dp_id" jdbcType="VARCHAR" /> <result column="person_sort" property="person_sort" jdbcType="VARCHAR" /> <result column="del_tag" property="del_tag" jdbcType="VARCHAR" /> </resultMap> <resultMap id="OrgResultMap" type="com.neusoft.icelake.modules.standard.entity.Org"> <id column="child_id" property="child_id" jdbcType="VARCHAR" /> <result column="child_desc" property="child_desc" jdbcType="VARCHAR" /> <result column="child_short" property="child_short" jdbcType="VARCHAR" /> <result column="parent_id" property="parent_id" jdbcType="VARCHAR" /> <result column="parent_desc" property="parent_desc" jdbcType="VARCHAR" /> <result column="org_sort" property="org_sort" jdbcType="VARCHAR" /> </resultMap> <sql id="Emp_Column_List"> accno, truename, gender, office_mail, title, dp_id, person_sort, del_tag </sql> <sql id="Org_Column_List"> parent_id, parent_desc, child_id, child_desc, child_short, org_sort </sql> <select id="getEmpAll" resultMap="EmpResultMap"> select <include refid="Emp_Column_List" /> from v_person </select> <select id="getOrgAll" resultMap="OrgResultMap"> select <include refid="Org_Column_List" /> from v_sap_org_node </select> </mapper>
package com.neusoft.icelake.modules.standard.service; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.neusoft.icelake.modules.standard.entity.Emps; import com.neusoft.icelake.modules.standard.entity.Orgs; @Path("/wq") public interface IWholeQuantityService { @GET @Path("/orglist") @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Orgs getOrgList(); @GET @Path("/emplist") @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Emps getEmpList(); }
package com.neusoft.icelake.modules.standard.service.impl; import java.util.List; import javax.inject.Named; import org.springframework.beans.factory.annotation.Autowired; import com.neusoft.icelake.modules.standard.dao.IWholeQuantityDao; import com.neusoft.icelake.modules.standard.entity.Emp; import com.neusoft.icelake.modules.standard.entity.Emps; import com.neusoft.icelake.modules.standard.entity.Org; import com.neusoft.icelake.modules.standard.entity.Orgs; import com.neusoft.icelake.modules.standard.service.IWholeQuantityService; @Named("wholeQuantityService") public class WholeQuantityServiceImpl implements IWholeQuantityService { private IWholeQuantityDao wholeQuantityDao; public IWholeQuantityDao getWholeQuantityDao() { return wholeQuantityDao; } @Autowired public void setWholeQuantityDao(IWholeQuantityDao wholeQuantityDao) { this.wholeQuantityDao = wholeQuantityDao; } @Override public Orgs getOrgList() { Orgs orgs = new Orgs(); List<Org> orglist = wholeQuantityDao.getOrgAll(); orgs.setList(orglist); return orgs; } @Override public Emps getEmpList() { Emps emps = new Emps(); List<Emp> emplist = wholeQuantityDao.getEmpAll(); emps.setList(emplist); return emps; } }
运行项目:
打开浏览器,在地址栏输入:http://localhost:8080/restservice/rest?_wadl
可以看到我们发布的webservice的接口描述文件(wadl描述):
访问http://localhost:8080/restservice/rest/wq/orglist?_type=json,返回Json结果,
或者http://localhost:8080/restservice/rest/wq/orglist?_type=xml,返回XML
源码下载:http://download.csdn.net/detail/iamlake/9498889
基于SSM+CXF构建的RESTFul webservice
标签:
原文地址:http://blog.csdn.net/iamlake/article/details/51217303