###開發環境 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