CXF实现REST接口开发
这一篇来实现REST开发接口。
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.qqw</groupId>
- <artifactId>CXF_Spring_restful</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>CXF_Spring_restful</name>
- <url>http://maven.apache.org</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.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>CXF_Spring_restful</display-name>
- <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>
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>CXF_Spring_restful.root</param-value>
- </context-param>
- <!-- 处理编码格式 -->
- <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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>characterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- Log4J Start -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>6000</param-value>
- </context-param>
- <!-- Spring Log4J config -->
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- 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>
- <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>/webservice/*</url-pattern>
- </servlet-mapping>
- </web-app>
3:applicationContext.xml和之前的不一样
- <?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:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx"
- <span style="color:#FF0000;"> </span>xmlns:jaxrs="http://cxf.apache.org/jaxrs"
- xsi:schemaLocation="
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
- <!-- 自动扫描 -->
- <context:component-scan base-package="com.qqw" />
- <!-- 定义服务提供者 -->
- <jaxrs:server id="webService" address="/">
- <!--输入拦截器设置-->
- <jaxrs:inInterceptors>
- <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
- </jaxrs:inInterceptors>
- <!--输出拦截器设置-->
- <jaxrs:outInterceptors>
- <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
- </jaxrs:outInterceptors>
- <!--serviceBeans:暴露的WebService服务类-->
- <jaxrs:serviceBeans>
- <ref bean="studentService"/>
- </jaxrs:serviceBeans>
- <!--支持的协议-->
- <jaxrs:extensionMappings>
- <entry key="json" value="application/json"/>
- <entry key="xml" value="application/xml"/>
- </jaxrs:extensionMappings>
- <!--编码格式-->
- <jaxrs:languageMappings>
- </jaxrs:languageMappings>
- <!--对象转换-->
- <jaxrs:providers>
- <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
- </jaxrs:providers>
- </jaxrs:server>
- </beans>
4:一个实体类
- package com.qqw.entity;
- public class Student {
- private String name;
- private String sex;
- private Integer age;
- public Student() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Student(String name, String sex, Integer age) {
- super();
- this.name = name;
- this.sex = sex;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- }
5:在写接口前一些注解的概念参考这篇文章:jax-rs常用注解
接口StudentService.Java
- package com.qqw.webservice;
- import java.util.List;
- 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 com.qqw.entity.Student;
- @Path("/studentService")
- @Produces(MediaType.APPLICATION_JSON)
- public interface StudentService {
- //参看是否有这个学生
- @GET
- @Path("/status/{name}/{age}")
- public String getStatus(@PathParam("name") String name,@PathParam("age") Integer age);
- //通过id查看学生信息
- @GET
- @Path("/student/{index}")
- public Student getStudentById(@PathParam("index") Integer id);
- //查看所有学生信息
- @GET
- @Path("/student")
- public List<Student> getStudentList();
- }
6:接口实现类
- package com.qqw.webserviceImpl;
- import java.util.List;
- import java.util.ArrayList;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- 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 {
- @Override
- public Student getStudentById( Integer id) {
- Student student=null;
- if(id==1){
- student=new Student("张三", "男", 20);
- }else{
- student=new Student("李四娘", "女", 30);
- }
- return student;
- }
- @Override
- 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);
- list.add(student);
- list.add(student1);
- list.add(student2);
- list.add(student3);
- list.add(student4);
- return list;
- }
- @Override
- public String getStatus(String name, Integer age) {
- String flag="FAULSE";
- if("tom狗".equals(name) && age==20){
- flag="TRUE";
- }
- return flag;
- }
- }
至此项目与spring整合完毕,把项目部署到tomcat。
测试第一个接口:http://localhost:8080/CXF_Spring_restful/webservice/studentService/status/tom狗/30,结果:
控制台信息:
测试第二个接口:
第三个接口: