Apache CXF + Spring3 + REST + JSON配置

来源:互联网 发布:中国远征军知乎 编辑:程序博客网 时间:2024/05/21 06:23

转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/48155331

1.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" 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 Rest Services</display-name>  <welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>      <servlet>        <servlet-name>CXFServlet</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>CXFServlet</servlet-name>        <url-pattern>/*</url-pattern>    </servlet-mapping>         <!-- 设置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></web-app>

2.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"default-lazy-init="true"><!-- 包扫描、注解相关 --><context:component-scan base-package="com.example"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><import resource="classpath:applicationContext-cxf.xml" />    </beans>

3.applicationContext-cxf.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:jaxws="http://cxf.apache.org/jaxws"    xmlns:jaxrs="http://cxf.apache.org/jaxrs"    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"    xsi:schemaLocation="      http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans.xsd      http://cxf.apache.org/jaxrs      http://cxf.apache.org/schemas/jaxrs.xsd      http://cxf.apache.org/transports/http/configuration      http://cxf.apache.org/schemas/configuration/http-conf.xsd      http://cxf.apache.org/jaxws      http://cxf.apache.org/schemas/jaxws.xsd">          <import resource="classpath:META-INF/cxf/cxf.xml" />    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    <http-conf:conduit           name="*.http-conduit">      <http-conf:client AllowChunking="false"/>  </http-conf:conduit>       <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">    <property name="dropRootElement" value="true"/>    <property name="dropCollectionWrapperElement" value="true"/>    </bean>        <jaxrs:server id="serviceContainer" address="/rsapi">        <jaxrs:serviceBeans>        <ref bean="testService" />        </jaxrs:serviceBeans>        <jaxrs:providers>        <ref bean="jsonProvider" />        </jaxrs:providers>        <jaxrs:languageMappings>        <entry key="en" value="en-gb" />        </jaxrs:languageMappings>    </jaxrs:server></beans>

4.CXFDemoImpl.java
@Service("testService")public class CXFDemoImpl implements CXFDemo {@Overridepublic String sayHello(final String foo) {return "hello " + foo;}@Overridepublic String sayHi() {return "Hi";}@Overridepublic TestObject getObject() {final TestObject obj = new TestObject();obj.setId(12);obj.setName("name1");obj.setDescription("this is a description.");final List<TestObject> fris = new ArrayList<>();TestObject obj1 = new TestObject();obj1.setId(13);obj1.setName("name2");obj1.setDescription("this is a description.");fris.add(obj1);obj1 = new TestObject();obj1.setId(14);obj1.setName("name3");obj1.setDescription("this is a description.");fris.add(obj1);obj.setFriends(fris);return obj;}}
5.CXFDemo.java

@Path("/test/")public interface CXFDemo {@GET@Path("/sayHello/{foo}")@Produces({ MediaType.APPLICATION_JSON })public String sayHello(@PathParam("foo") String foo);@GET@Path("/sayhi")@Produces({ MediaType.APPLICATION_JSON })public String sayHi();@GET@Path("/getobject")@Produces({ MediaType.APPLICATION_JSON })public TestObject getObject();}
6.TestObject.java

@XmlRootElement(name = "test")public class TestObject {private int id;private String name;private String description;private List<TestObject> friends;public int getId() {return this.id;}public void setId(final int id) {this.id = id;}public String getName() {return this.name;}public void setName(final String name) {this.name = name;}public String getDescription() {return this.description;}public void setDescription(final String description) {this.description = description;}public List<TestObject> getFriends() {return this.friends;}public void setFriends(final List<TestObject> friends) {this.friends = friends;}}

部署到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"}

代码下载:http://download.csdn.net/detail/goldenfish1919/9069737


0 0