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
- Apache CXF + Spring3 + REST + JSON配置
- Apache CXF REST 文件上传,form提交和json提交。
- Apache CXF REST example
- Apache CXF REST example
- Apache CXF REST example
- Apache CXF REST api
- cxf-rest 配置JSonProvider 来定制JSON的转换格式
- CXF REST/JSON文件上传
- spring+cxf+maven+rest风格+json/xml
- Apache CXF配置
- CXF Rest
- Spring3 MVC 笔记(二) —json+rest优化
- 使用Apache CXF搭建REST风格的Web Service
- apache-cxf-3.0.4和spring实现rest服务
- Apache CXF REST 文件上传,中文乱码(已解决)
- 如何部署 apache CXF rest app to Weblogic
- Apache Camel配置SSL的CXF
- 配置apache cxf 客户端支持https
- Bootstrap学习: 表格
- Delphi中Chrome Chromium、Cef3学习笔记(四)
- mysql 基础语法知识
- jQuery Validate
- 正则表达式30分钟入门教程
- Apache CXF + Spring3 + REST + JSON配置
- 极光推送 cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY 错误解决方法
- 'Push segues can only be used when the source controller is managed by an instance of UINavigationC
- Remote debug 参数
- Bootstrap学习:表单
- Altium Designer笔记之PCB工作层
- POJ 1401:Factorial 求一个数阶乘的末尾0的个数
- spring4.x注解概述
- VI 基本使用手册