这一篇来实现REST开发接口,具体的概念以及和SOAP的区别参考上一篇文章。
1:pom.xml
<project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qqw</groupId>
<artifactId>CXF_Spring_restful</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CXF_Spring_restful</name>
<url>http://maven.</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 添加Spring支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 添加cxf支持 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.5</version>
</dependency>
//项目里面所有的注解在这个api包下面
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
//这是json处理的包,目前这知道这个,GSON和fastjson没尝试
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.1.4</version>
</dependency>
<!--实现日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
</dependencies>
</project>
2:web.xml这个和之前一样,没什么变化
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns="http://java./xml/ns/javaee" xmlns:web="http://java./xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>CXF_Spring_restful</display-name> <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> <param-name>webAppRootKey</param-name> <param-value>CXF_Spring_restful.root</param-value> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <param-name>encoding</param-name> <param-value>UTF-8</param-value> <param-name>forceEncoding</param-name> <param-value>true</param-value> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> <!-- Spring Log4J config --> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern>
3:applicationContext.xml和之前的不一样
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:p="http://www./schema/p" xmlns:aop="http://www./schema/aop" xmlns:context="http://www./schema/context" xmlns:jee="http://www./schema/jee" xmlns:tx="http://www./schema/tx" xmlns:jaxrs="http://cxf./jaxrs" http://www./schema/aop http://www./schema/aop/spring-aop-4.0.xsd http://www./schema/beans http://www./schema/beans/spring-beans-4.0.xsd http://www./schema/context http://www./schema/context/spring-context-4.0.xsd http://www./schema/jee http://www./schema/jee/spring-jee-4.0.xsd http://www./schema/tx http://www./schema/tx/spring-tx-4.0.xsd http://cxf./jaxrs http://cxf./schemas/jaxrs.xsd"> <context:component-scan base-package="com.qqw" /> <jaxrs:server id="webService" address="/"> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <!--serviceBeans:暴露的WebService服务类--> <ref bean="studentService"/> <jaxrs:extensionMappings> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> </jaxrs:extensionMappings> </jaxrs:languageMappings> <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
4:一个实体类
// TODO Auto-generated constructor stub public Student(String name, String sex, Integer age) { public String getName() { public void setName(String name) { public void setSex(String sex) { public Integer getAge() { public void setAge(Integer age) {
5:在写接口前一些注解的概念参考这篇文章:
JAX-RS规范-常用注解浅析
接口StudentService.java
package com.qqw.webservice; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.qqw.entity.Student; @Produces(MediaType.APPLICATION_JSON) public interface StudentService { @Path("/status/{name}/{age}") public String getStatus(@PathParam("name") String name,@PathParam("age") Integer age); @Path("/student/{index}") public Student getStudentById(@PathParam("index") Integer id); public List<Student> getStudentList();
6:接口实现类
package com.qqw.webserviceImpl; import java.util.ArrayList; import javax.ws.rs.PathParam; import org.springframework.stereotype.Component; import com.qqw.entity.Student; import com.qqw.webservice.StudentService; @Component("studentService") public class StudenntServiceImpl implements StudentService { public Student getStudentById( Integer id) { student=new Student("张三", "男", 20); student=new Student("李四娘", "女", 30); public List<Student> getStudentList() { List<Student> list=new ArrayList<Student>(); Student student=new Student("张三", "男", 20); Student student1=new Student("李四娘", "女", 30); Student student2=new Student("李1娘", "女", 31); Student student3=new Student("李2娘", "女", 32); Student student4=new Student("李3娘", "女", 20); public String getStatus(String name, Integer age) { if("tom狗".equals(name) && age==20){
至此项目与spring整合完毕,把项目部署到tomcat。
测试第一个接口:http://localhost:8080/CXF_Spring_restful/webservice/studentService/status/tom狗/30,结果:
控制台信息:
测试第二个接口:
第三个接口:
到此,CXF系列完毕!!!完整代码
|