###开发环境 Maven Eclipse Tomcathtml
###快速构建 archetype太少?关于如何添加本地archetype catalog:java
下载文件 http://repo1.maven.org/maven2/archetype-catalog.xml 设置Eclipse 首选项 Maven Archetype ,将archetype-catalog.xml 添加为Local catalog.web
使用Archetype 建立Maven项目,坐标:spring
org.apache.cxf.archetypeapache
cxf-jaxrs-service 3.1.4json
注释掉pom.xml文件中的插件配置,执行 mvn clean install
将项目发布到Tomcat服务器
个人项目名称为 my_cxf_restful
访问URL http://localhost:8080/my_cxf_restful/hello/echo/cong 网页显示结果为:服务器
congrestful
###项目构建步骤 ####导入依赖包 导入jackson 是由于须要处理json类型数据
cxf-rt-frontend-jaxrs 为CXF 开发RESTful WebService 必须的包,其它依赖Maven会自动处理
项目基于Springapp
<properties> <jackson.version>1.8.6</jackson.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
####编写RESTful WebService类: 这里并无写成接口,而是直接写成实现类.
modifyJson方法中框架会将POST请求体的JSON 数据转换成对象JsonBean
注解说明,见下一条目框架
@Path("/hello") public class HelloWorld { @GET @Path("/echo/{input}") @Produces("text/plain") public String ping(@PathParam("input") String input) { return input; } @POST @Produces("application/json") @Consumes("application/json") @Path("/jsonBean") public Response modifyJson(JsonBean input) { input.setVal2(input.getVal1()); return Response.ok().entity(input).build(); } }
####注解说明: 本文段引用自http://www.ibm.com/developerworks/cn/opensource/os-restfulwebservices/index.html
(天啊!竟然不支持表格)
####Spring Bean 配置:
<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <context:property-placeholder/> <context:annotation-config/> <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"/> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/> <jaxrs:server id="services" address="/"> <jaxrs:serviceBeans> <bean class="zhwc.example.my_cxf_restful.HelloWorld"/> </jaxrs:serviceBeans> <jaxrs:providers> <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/> </jaxrs:providers> </jaxrs:server> </beans>
####Web.xml配置: 与构建一个简单的WebService同样: 使用Spring监听器加载BEAN配置文件;
注册CXF Servlet处理请求.
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>JAX-RS Simple Service</display-name> <description>JAX-RS Simple Service</description> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/beans.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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
####测试
使用FireFox插件Open HTTPRequester post发送json数据
使用方法:使用firefox插件httperrequest,模拟发送及接收Json请求
####更多实例 下载Apache CXF 发布包
查看${CXF_HOME}/simple
例如 ...\apache-cxf-3.1.4\samples\jax_rs\basic
###参考资料: http://www.ibm.com/developerworks/cn/opensource/os-restfulwebservices/index.html
http://www.ibm.com/developerworks/cn/webservices/ws-pojo-springcxf2/index.html