最簡單的cxf3.1的helloword入門例子
轉載請標明出處:
http://blog.csdn.net/ouyida3/article/details/46045261
本文出自:【ouyida3的部落格】 一、為什麼要寫這篇文章
官網的user guide指引文章是英文,難懂。即使看懂了,裡面的代碼範例也是丟三落四,有一定基礎的人還好,沒有基礎的人很難根據那些代碼讓程式跑得起來。百度搜尋了一些技術部落格,搜尋結果第一頁裡我都試了,都跑不起來,原因都是缺這或缺那,不適合初學者。 二、服務端 1、web.xml配置
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</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/config/applicationContext.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>/cxf/*</url-pattern> </servlet-mapping></web-app>
PS:建議使用WEB伺服器 2、spring配置
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:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <bean id="hello" class="demo.spring.service.HelloWorldImpl" /> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /></beans>
PS:建議使用spring 3、介面類
HelloWorld.java
package demo.spring.service;import javax.jws.WebService;@WebServicepublic interface HelloWorld { String sayHi(String text);}4、實作類別
HelloWorldImpl.java
package demo.spring.service;import javax.jws.WebService;@WebService(endpointInterface = "demo.spring.service.HelloWorld")public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; }}5、WEB伺服器配置
tomcat的server.xml增加
<Context path="/demo" docBase="C:\Documents and Settings\Administrator\Workspaces\MyEclipse Professional 2014\cxf\WebRoot" debug="5" reloadable="true" crossContext="true"></Context>
6、完成、測試
啟動tomcat,瀏覽器輸入:
http://localhost:8080/demo/cxf
三、用戶端
腳註1 1、不使用spring
Clinet.java
package demo.spring.service;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:8080/demo/cxf/HelloWorld"); HelloWorld hw = (HelloWorld) svr.create(); hw.sayHi("abc"); }}
五月 26, 2015 3:31:00 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
資訊: Creating Service {http://service.spring.demo/}HelloWorldService from class demo.spring.service.HelloWorld
sayHi called
地址http://localhost:8080/demo/cxf/HelloWorld可在瀏覽器中開啟。 2、使用spring
ClientWithSpring.java
package demo.spring.service;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ClientWithSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("config/applicationContext.xml"); HelloWorld client = (HelloWorld) context.getBean("client"); client.sayHi(""); }}
applicationContext.xml增加
<bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="demo.spring.service.HelloWorld"/> <property name="address" value="http://localhost:8080/demo/cxf/HelloWorld"/></bean>
用戶端日誌
服務端日誌
參考:
http://cxf.apache.org/docs/writing-a-service-with-spring.html
幾個要點:
1. 官網裡的web.xml漏了cxf的配置
2. 官網裡沒有描述瀏覽器應該輸入什麼url來測試服務是否已啟動/可用
3. 官網裡client沒有描述地址應該怎麼填
4. 最好用servlet容器,比如tomcat,不用應該也可以,因為內建了jetty,試了很久浪費了很多時間
5. 最好結合spring,不用spring試了很久浪費了很多時間
6. 把apache-cxf-3.1.0.zip裡lib下的全部jar拷貝到WEB-INF/lib,不全部拷貝沒試過
7. 還有java2wsdl2,QName麻煩一點,下篇文章再講
ouyida3’s blog
2015.5.27 這裡說的用戶端只是測試服務端的用戶端,他們是部署在同一個容器裡,所以可以直接引用服務端中的類。真實的情境是服務端提供wsdl,用戶端根據wsdl產生java檔案,再開發用戶端代碼。 java2wsdl就是上面說的真正的用戶端中所必要的