Apache CXF + Spring3 + REST + JSON配置

JAX-RS提供了一些标注将一个资源类,一个POJOJava类,封装为Web资源。标注包括:

(1)、@Path,标注资源类或方法的相对路径

(2)、@GET,@PUT,@POST,@DELETE,标注方法是用的HTTP请求的类型

(3)、@Produces,标注返回的MIME媒体类型,( 注解标注,这个注解可以包含一组字符串,默认值是*/*,它指定REST 服务的响应结果的MIME 类型,例如:application/xml、application/json、image/jpeg 等),你 也可以同时返回多种类型,但具体生成结果时使用哪种格式取决于ContentType。CXF 默认返回的是JSON 字符串。

(4)、@PathParam,@QueryParam,@HeaderParam,@CookieParam,@MatrixParam,@FormParam,分别标注方法的参数来自于HTTP请求的不同位置,例如@PathParam来自于URL的路径,@QueryParam来自于URL的查询参数,@HeaderParam来自于HTTP请求的头信息,@CookieParam来自于HTTP请求的Cookie。

1.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" 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 Rest Services</display-name>  
  4.     
  5.     <welcome-file-list>  
  6.         <welcome-file>index.html</welcome-file>  
  7.     </welcome-file-list>  
  8.       
  9.       
  10.     <servlet>  
  11.         <servlet-name>CXFServlet</servlet-name>  
  12.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  13.         <load-on-startup>1</load-on-startup>  
  14.     </servlet>  
  15.     <servlet-mapping>  
  16.         <servlet-name>CXFServlet</servlet-name>  
  17.         <url-pattern>/*</url-pattern>  
  18.     </servlet-mapping>   
  19.       
  20.     <!-- 设置spring容器加载配置文件的路径 -->  
  21.     <context-param>  
  22.         <param-name>contextConfigLocation</param-name>  
  23.         <param-value>classpath:applicationContext.xml</param-value>  
  24.     </context-param>  
  25.     <listener>  
  26.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  27.     </listener>  
  28.       
  29. </web-app>  


2.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" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:cache="http://www.springframework.org/schema/cache"  
  7.     xsi:schemaLocation="  
  8.      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
  11.      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  12.      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  13.      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd  
  14.      http://www.springframework.org/schema/cache  
  15.      http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"  
  16.     default-lazy-init="true">  
  17.       
  18.       
  19.     <!-- 包扫描、注解相关 -->  
  20.     <context:component-scan base-package="com.example">  
  21.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  22.     </context:component-scan>  
  23.       
  24.     <import resource="classpath:applicationContext-cxf.xml" />  
  25.       
  26. </beans>  


3.applicationContext-cxf.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:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
  6.     xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"  
  7.     xsi:schemaLocation="  
  8.       http://www.springframework.org/schema/beans  
  9.       http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.       http://cxf.apache.org/jaxrs  
  11.       http://cxf.apache.org/schemas/jaxrs.xsd  
  12.       http://cxf.apache.org/transports/http/configuration  
  13.       http://cxf.apache.org/schemas/configuration/http-conf.xsd  
  14.       http://cxf.apache.org/jaxws  
  15.       http://cxf.apache.org/schemas/jaxws.xsd">  
  16.         
  17.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  18.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  19.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  20.     <http-conf:conduit  
  21.            name="*.http-conduit">  
  22.       <http-conf:client AllowChunking="false"/>  
  23.     </http-conf:conduit>  
  24.        
  25.     <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">  
  26.         <property name="dropRootElement" value="true"/>  
  27.         <property name="dropCollectionWrapperElement" value="true"/>  
  28.     </bean>  
  29.       
  30.     <jaxrs:server id="serviceContainer" address="/rsapi">  
  31.         <jaxrs:serviceBeans>  
  32.             <ref bean="testService" />  
  33.         </jaxrs:serviceBeans>  
  34.         <jaxrs:providers>  
  35.             <ref bean="jsonProvider" />  
  36.         </jaxrs:providers>  
  37.         <jaxrs:languageMappings>  
  38.             <entry key="en" value="en-gb" />  
  39.         </jaxrs:languageMappings>  
  40.     </jaxrs:server>  
  41. </beans>  


4.CXFDemoImpl.java

[java] view plain copy
 
  1. @Service("testService")  
  2. public class CXFDemoImpl implements CXFDemo {  
  3.     @Override  
  4.     public String sayHello(final String foo) {  
  5.         return "hello " + foo;  
  6.     }  
  7.   
  8.     @Override  
  9.     public String sayHi() {  
  10.   
  11.         return "Hi";  
  12.     }  
  13.   
  14.     @Override  
  15.     public TestObject getObject() {  
  16.         final TestObject obj = new TestObject();  
  17.         obj.setId(12);  
  18.         obj.setName("name1");  
  19.         obj.setDescription("this is a description.");  
  20.         final List<TestObject> fris = new ArrayList<>();  
  21.   
  22.         TestObject obj1 = new TestObject();  
  23.         obj1.setId(13);  
  24.         obj1.setName("name2");  
  25.         obj1.setDescription("this is a description.");  
  26.         fris.add(obj1);  
  27.   
  28.         obj1 = new TestObject();  
  29.         obj1.setId(14);  
  30.         obj1.setName("name3");  
  31.         obj1.setDescription("this is a description.");  
  32.         fris.add(obj1);  
  33.         obj.setFriends(fris);  
  34.         return obj;  
  35.     }  
  36. }  

5.CXFDemo.java

[java] view plain copy
 
  1. @Path("/test/")  
  2. public interface CXFDemo {  
  3.     @GET  
  4.     @Path("/sayHello/{foo}")  
  5.     @Produces({ MediaType.APPLICATION_JSON })  
  6.     public String sayHello(@PathParam("foo") String foo);  
  7.   
  8.     @GET  
  9.     @Path("/sayhi")  
  10.     @Produces({ MediaType.APPLICATION_JSON })  
  11.     public String sayHi();  
  12.   
  13.     @GET  
  14.     @Path("/getobject")  
  15.     @Produces({ MediaType.APPLICATION_JSON })  
  16.     public TestObject getObject();  
  17. }  

6.TestObject.java

[java] view plain copy
 
  1. @XmlRootElement(name = "test")  
  2. public class TestObject {  
  3.   
  4.     private int id;  
  5.     private String name;  
  6.     private String description;  
  7.     private List<TestObject> friends;  
  8.   
  9.     public int getId() {  
  10.         return this.id;  
  11.     }  
  12.   
  13.     public void setId(final int id) {  
  14.         this.id = id;  
  15.     }  
  16.   
  17.     public String getName() {  
  18.         return this.name;  
  19.     }  
  20.   
  21.     public void setName(final String name) {  
  22.         this.name = name;  
  23.     }  
  24.   
  25.     public String getDescription() {  
  26.         return this.description;  
  27.     }  
  28.   
  29.     public void setDescription(final String description) {  
  30.         this.description = description;  
  31.     }  
  32.   
  33.     public List<TestObject> getFriends() {  
  34.         return this.friends;  
  35.     }  
  36.   
  37.     public void setFriends(final List<TestObject> friends) {  
  38.         this.friends = friends;  
  39.     }  
  40. }  


部署到tomcat,访问:http://localhost:8080/rest_cxf/rsapi/test/getobject

输出:

{"description":"this is a description.","friends":[{"description":"this is a description.","id":13,"name":"name2"},{"description":"this is a description.","id":14,"name":"name3"}],"id":12,"name":"name1"}
原文地址:https://www.cnblogs.com/baorantHome/p/7387821.html