Spring 4 集成Apache CXF开发JAX-RS Web Service
什么是JAX-RS
在JSR-311规范中定义,即Java API for RESTful Web Services,一套Java API,用于开发 RESTful风格的Webservice。
工程概况
主要功能:用户增删改查controller(not RESTful,懒得改了) + 查询用户webservice(RESTful)
框架:spring+spring mvc+mybatis
数据库:mysql
构建工具:maven
创建工程
使用IDE创建一个maven工程,pom.xml文件如下,引入cxf、spring、mybatis等依赖,添加tomcat-maven插件:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.edu.hdu.wsdemo</groupId> <artifactId>wsdemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>wsdemo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <cxf.version>3.0.3</cxf.version> <httpclient.version>3.1</httpclient.version> <jax.ws.rs>2.0.1</jax.ws.rs> <spring.version>4.1.4.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <jackson.version>1.1.1</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <!-- Simple Logging Facade for Java,简单日志门面 ,允许最终用户在部署其应用时使用其所希望的日志系统,本身并不是日志解决方案 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <!-- http://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- http://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> <!-- http://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!--spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.15</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.5</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-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-service-description</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>${httpclient.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>${jax.ws.rs}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>${jackson.version}</version> </dependency> </dependencies> <build> <finalName>wsdemo</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> </plugin> </plugins> </build> </project>
关于spring+spring mvc+mybatis框架的相关配置就不细说了,这里主要关注与CXF相关的配置。
首先,是在web.xml添加一个servlet,拦截Web Service的请求:
<servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
然后,配置jaxrs:server:
<bean id="userWSService" class="cn.edu.hdu.wsdemo.wsservice.UserWSService"></bean> <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" /> <jaxrs:server id="users" address="/apis"> <jaxrs:serviceBeans> <ref bean="userWSService" /> </jaxrs:serviceBeans> <jaxrs:providers> <ref bean='jsonProvider' /> </jaxrs:providers> </jaxrs:server>
最后,编写我们的webservice具体实现:
package cn.edu.hdu.wsdemo.wsservice; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import cn.edu.hdu.wsdemo.model.User; import cn.edu.hdu.wsdemo.service.user.IUserService; @Path("/userwsservice/") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class UserWSService { @Autowired private IUserService userService; @GET @Path("/users/{id}/") public User getUser(@PathParam("id") String id) { Map<String, Object> param = new HashMap<String, Object>(); param.put("id", id); List<User> userList = userService.query(param); return userList.size() > 0 ? userList.get(0) : null; } @POST @Path("/users/getall") public List<User> getAllUsers() { List<User> userList = userService.query(new HashMap<String, Object>()); return userList; } }
测试服务
执行maven命令:clean tomcat:run
启动成功后,浏览器输入:http://localhost:8080/wsdemo/ws
结果:
输入:http://localhost:8080/wsdemo/ws/apis?_wadl
结果:
输入:http://localhost:8080/wsdemo/ws/apis/userwsservice/users/4
结果:
源码
https://github.com/peterchenhdu/demos/tree/master/wsdemo
参考资料