• CXF实现REST接口开发


    这一篇来实现REST开发接口。

    1:pom.xml

    [plain] view plain copy
     
    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    3.   <modelVersion>4.0.0</modelVersion>  
    4.   
    5.   <groupId>com.qqw</groupId>  
    6.   <artifactId>CXF_Spring_restful</artifactId>  
    7.   <version>0.0.1-SNAPSHOT</version>  
    8.   <packaging>jar</packaging>  
    9.   
    10.   <name>CXF_Spring_restful</name>  
    11.   <url>http://maven.apache.org</url>  
    12.   
    13.   <properties>  
    14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    15.   </properties>  
    16.   
    17.   <dependencies>  
    18.     <dependency>  
    19.       <groupId>junit</groupId>  
    20.       <artifactId>junit</artifactId>  
    21.       <version>3.8.1</version>  
    22.       <scope>test</scope>  
    23.     </dependency>  
    24.       
    25.     <!-- 添加Spring支持 -->  
    26.     <dependency>  
    27.         <groupId>org.springframework</groupId>  
    28.         <artifactId>spring-core</artifactId>  
    29.         <version>4.1.7.RELEASE</version>  
    30.     </dependency>  
    31.     <dependency>  
    32.         <groupId>org.springframework</groupId>  
    33.         <artifactId>spring-beans</artifactId>  
    34.         <version>4.1.7.RELEASE</version>  
    35.     </dependency>  
    36.     <dependency>  
    37.          <groupId>org.springframework</groupId>  
    38.          <artifactId>spring-tx</artifactId>  
    39.          <version>4.1.7.RELEASE</version>  
    40.         </dependency>  
    41.     <dependency>  
    42.         <groupId>org.springframework</groupId>  
    43.         <artifactId>spring-context</artifactId>  
    44.         <version>4.1.7.RELEASE</version>  
    45.     </dependency>  
    46.     <dependency>  
    47.         <groupId>org.springframework</groupId>  
    48.         <artifactId>spring-context-support</artifactId>  
    49.         <version>4.1.7.RELEASE</version>  
    50.     </dependency>  
    51.        
    52.     <dependency>  
    53.         <groupId>org.springframework</groupId>  
    54.         <artifactId>spring-web</artifactId>  
    55.         <version>4.1.7.RELEASE</version>  
    56.     </dependency>  
    57.        
    58.     <dependency>  
    59.         <groupId>org.springframework</groupId>  
    60.         <artifactId>spring-webmvc</artifactId>  
    61.         <version>4.1.7.RELEASE</version>  
    62.     </dependency>  
    63.        
    64.     <dependency>  
    65.         <groupId>org.springframework</groupId>  
    66.         <artifactId>spring-aop</artifactId>  
    67.         <version>4.1.7.RELEASE</version>  
    68.     </dependency>  
    69.        
    70.        
    71.     <dependency>  
    72.         <groupId>org.springframework</groupId>  
    73.         <artifactId>spring-aspects</artifactId>  
    74.         <version>4.1.7.RELEASE</version>  
    75.     </dependency>  
    76.        
    77.     <dependency>  
    78.         <groupId>org.springframework</groupId>  
    79.         <artifactId>spring-jdbc</artifactId>  
    80.         <version>4.1.7.RELEASE</version>  
    81.     </dependency>  
    82.       
    83.       
    84.     <!-- 添加cxf支持  -->  
    85.        
    86.     <dependency>  
    87.         <groupId>org.apache.cxf</groupId>  
    88.         <artifactId>cxf-rt-transports-http</artifactId>  
    89.         <version>3.1.5</version>  
    90.     </dependency>  
    91.         //项目里面所有的注解在这个api包下面  
    92.     <dependency>  
    93.         <groupId>javax.ws.rs</groupId>  
    94.         <artifactId>javax.ws.rs-api</artifactId>  
    95.         <version>2.0.1</version>  
    96.     </dependency>  
    97.     //这是json处理的包,目前这知道这个,GSON和fastjson没尝试  
    98.     <dependency>  
    99.         <groupId>org.codehaus.jackson</groupId>  
    100.         <artifactId>jackson-jaxrs</artifactId>  
    101.         <version>1.9.13</version>  
    102.     </dependency>  
    103.       
    104.     <dependency>  
    105.         <groupId>org.apache.cxf</groupId>  
    106.         <artifactId>cxf-rt-frontend-jaxrs</artifactId>  
    107.         <version>3.1.4</version>  
    108.     </dependency>  
    109.       
    110.     <!--实现日志  -->  
    111.     <dependency>  
    112.         <groupId>org.slf4j</groupId>  
    113.         <artifactId>slf4j-log4j12</artifactId>  
    114.         <version>1.7.22</version>  
    115.     </dependency>  
    116.       
    117.       
    118.   </dependencies>  
    119. </project>  

    2:web.xml这个和之前一样,没什么变化

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <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">  
    3.   <display-name>CXF_Spring_restful</display-name>  
    4.   <welcome-file-list>  
    5.     <welcome-file>index.html</welcome-file>  
    6.     <welcome-file>index.htm</welcome-file>  
    7.     <welcome-file>index.jsp</welcome-file>  
    8.     <welcome-file>default.html</welcome-file>  
    9.     <welcome-file>default.htm</welcome-file>  
    10.     <welcome-file>default.jsp</welcome-file>  
    11.   </welcome-file-list>  
    12.     
    13.     <context-param>  
    14.         <param-name>webAppRootKey</param-name>  
    15.         <param-value>CXF_Spring_restful.root</param-value>  
    16.     </context-param>  
    17.   <!-- 处理编码格式 -->  
    18.     <filter>    
    19.         <filter-name>characterEncodingFilter</filter-name>    
    20.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
    21.         <init-param>    
    22.             <param-name>encoding</param-name>    
    23.             <param-value>UTF-8</param-value>    
    24.         </init-param>    
    25.         <init-param>    
    26.             <param-name>forceEncoding</param-name>    
    27.             <param-value>true</param-value>    
    28.         </init-param>    
    29.     </filter>    
    30.     <filter-mapping>    
    31.         <filter-name>characterEncodingFilter</filter-name>    
    32.         <url-pattern>/*</url-pattern>    
    33.     </filter-mapping>  
    34.       
    35.     <!-- Log4J Start -->  
    36.     <context-param>  
    37.         <param-name>log4jConfigLocation</param-name>  
    38.         <param-value>classpath:log4j.properties</param-value>  
    39.     </context-param>  
    40.     <context-param>  
    41.         <param-name>log4jRefreshInterval</param-name>  
    42.         <param-value>6000</param-value>  
    43.     </context-param>  
    44.     <!-- Spring Log4J config -->  
    45.     <listener>  
    46.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    47.     </listener>  
    48.       
    49.     <!-- Spring配置文件 -->  
    50.     <context-param>  
    51.         <param-name>contextConfigLocation</param-name>  
    52.         <param-value>classpath:applicationContext.xml</param-value>  
    53.     </context-param>  
    54.     <listener>  
    55.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    56.     </listener>  
    57.       
    58.     <servlet>    
    59.      <servlet-name>CXFServlet</servlet-name>    
    60.      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
    61.     </servlet>    
    62.        
    63.     <servlet-mapping>    
    64.        <servlet-name>CXFServlet</servlet-name>    
    65.        <url-pattern>/webservice/*</url-pattern>    
    66.     </servlet-mapping>  
    67.       
    68.     
    69. </web-app>  

    3:applicationContext.xml和之前的不一样

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>      
    2. <beans xmlns="http://www.springframework.org/schema/beans"      
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    4.     xmlns:p="http://www.springframework.org/schema/p"    
    5.     xmlns:aop="http://www.springframework.org/schema/aop"     
    6.     xmlns:context="http://www.springframework.org/schema/context"    
    7.     xmlns:jee="http://www.springframework.org/schema/jee"    
    8.     xmlns:tx="http://www.springframework.org/schema/tx"    
    9.    <span style="color:#FF0000;"</span>xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
    10.     xsi:schemaLocation="      
    11.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd    
    12.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
    13.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd    
    14.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd    
    15.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    16.         http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">      
    17.       
    18.     <!-- 自动扫描 -->  
    19.     <context:component-scan base-package="com.qqw" />  
    20.       
    21.     <!-- 定义服务提供者  -->  
    22.     <jaxrs:server id="webService" address="/">  
    23.         <!--输入拦截器设置-->  
    24.         <jaxrs:inInterceptors>  
    25.             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>  
    26.         </jaxrs:inInterceptors>  
    27.   
    28.         <!--输出拦截器设置-->  
    29.         <jaxrs:outInterceptors>  
    30.             <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>  
    31.         </jaxrs:outInterceptors>  
    32.           
    33.         <!--serviceBeans:暴露的WebService服务类-->  
    34.         <jaxrs:serviceBeans>  
    35.             <ref bean="studentService"/>  
    36.         </jaxrs:serviceBeans>  
    37.               
    38.         <!--支持的协议-->  
    39.         <jaxrs:extensionMappings>  
    40.             <entry key="json" value="application/json"/>  
    41.             <entry key="xml" value="application/xml"/>  
    42.         </jaxrs:extensionMappings>  
    43.         <!--编码格式-->  
    44.         <jaxrs:languageMappings>  
    45.               
    46.         </jaxrs:languageMappings>  
    47.         <!--对象转换-->  
    48.         <jaxrs:providers>  
    49.             <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>  
    50.         </jaxrs:providers>  
    51.           
    52.     </jaxrs:server>  
    53. </beans>  


    4:一个实体类

    [java] view plain copy
     
    1. package com.qqw.entity;  
    2.   
    3. public class Student {  
    4.   
    5.     private String name;  
    6.     private String sex;  
    7.     private Integer age;  
    8.     public Student() {  
    9.         super();  
    10.         // TODO Auto-generated constructor stub  
    11.     }  
    12.     public Student(String name, String sex, Integer age) {  
    13.         super();  
    14.         this.name = name;  
    15.         this.sex = sex;  
    16.         this.age = age;  
    17.     }  
    18.     public String getName() {  
    19.         return name;  
    20.     }  
    21.     public void setName(String name) {  
    22.         this.name = name;  
    23.     }  
    24.     public String getSex() {  
    25.         return sex;  
    26.     }  
    27.     public void setSex(String sex) {  
    28.         this.sex = sex;  
    29.     }  
    30.     public Integer getAge() {  
    31.         return age;  
    32.     }  
    33.     public void setAge(Integer age) {  
    34.         this.age = age;  
    35.     }  
    36.       
    37.       
    38.       
    39. }  


    5:在写接口前一些注解的概念参考这篇文章:jax-rs常用注解

    接口StudentService.Java

    [java] view plain copy
     
    1. package com.qqw.webservice;  
    2.   
    3. import java.util.List;  
    4.   
    5. import javax.ws.rs.GET;  
    6. import javax.ws.rs.Path;  
    7. import javax.ws.rs.PathParam;  
    8. import javax.ws.rs.Produces;  
    9. import javax.ws.rs.core.MediaType;  
    10.   
    11.   
    12. import com.qqw.entity.Student;  
    13.   
    14. @Path("/studentService")  
    15. @Produces(MediaType.APPLICATION_JSON)  
    16. public interface StudentService {  
    17.   
    18.     //参看是否有这个学生  
    19.     @GET  
    20.     @Path("/status/{name}/{age}")  
    21.     public String getStatus(@PathParam("name") String name,@PathParam("age") Integer age);  
    22.       
    23.     //通过id查看学生信息  
    24.     @GET  
    25.     @Path("/student/{index}")  
    26.     public Student getStudentById(@PathParam("index") Integer id);  
    27.       
    28.     //查看所有学生信息  
    29.     @GET  
    30.     @Path("/student")  
    31.     public List<Student> getStudentList();  
    32.       
    33. }  


    6:接口实现类

    [java] view plain copy
     
    1. package com.qqw.webserviceImpl;  
    2.   
    3. import java.util.List;  
    4. import java.util.ArrayList;  
    5.   
    6. import javax.ws.rs.GET;  
    7. import javax.ws.rs.Path;  
    8. import javax.ws.rs.PathParam;  
    9.   
    10. import org.springframework.stereotype.Component;  
    11.   
    12. import com.qqw.entity.Student;  
    13. import com.qqw.webservice.StudentService;  
    14.   
    15. @Component("studentService")  
    16. public class StudenntServiceImpl implements StudentService {  
    17.   
    18.       
    19.   
    20.     @Override  
    21.       
    22.     public Student getStudentById( Integer id) {  
    23.           
    24.         Student student=null;  
    25.         if(id==1){  
    26.             student=new Student("张三", "男", 20);  
    27.         }else{  
    28.             student=new Student("李四娘", "女", 30);  
    29.         }  
    30.         return student;  
    31.     }  
    32.   
    33.     @Override  
    34.     public List<Student> getStudentList() {  
    35.           
    36.         List<Student> list=new ArrayList<Student>();  
    37.         Student student=new Student("张三", "男", 20);  
    38.         Student student1=new Student("李四娘", "女", 30);  
    39.         Student student2=new Student("李1娘", "女", 31);  
    40.         Student student3=new Student("李2娘", "女", 32);  
    41.         Student student4=new Student("李3娘", "女", 20);  
    42.         list.add(student);  
    43.         list.add(student1);  
    44.         list.add(student2);  
    45.         list.add(student3);  
    46.         list.add(student4);  
    47.         return list;  
    48.     }  
    49.   
    50.     @Override  
    51.     public String getStatus(String name, Integer age) {  
    52.           
    53.         String flag="FAULSE";  
    54.         if("tom狗".equals(name) && age==20){  
    55.              flag="TRUE";  
    56.         }  
    57.         return flag;  
    58.     }  
    59.   
    60. }  


    至此项目与spring整合完毕,把项目部署到tomcat。

    测试第一个接口:http://localhost:8080/CXF_Spring_restful/webservice/studentService/status/tom狗/30,结果:

    控制台信息:

    测试第二个接口:

    第三个接口:

    到此,CXF系列完毕!!!

  • 相关阅读:
    外币折换金额修改配置文件
    账簿与平衡段关联表
    查询税则
    税配置后台表
    Information Center
    查询纳税账户
    职场动物进化手册 升级版
    Indistractable
    像玉的石头
    [Chicago guides to writing editing and publishing]
  • 原文地址:https://www.cnblogs.com/baorantHome/p/7410500.html
Copyright © 2020-2023  润新知