轉自 :
http://blog.chinaunix.net/u2/73798/showart_2002108.html
將wdl binder
http://service.test.com/替換爲本身的namespace。
命令格式以下:
wsdl2java -b jaxb-binding-date.xml http://localhost:8080/cxfservice/services/XXXXX?wsdl
#########################################################
CXF wsdl2Java
[img]/admin/templates/default/images/right_line.gif" border="0" alt="" width="502" height="9[/img]
一. 簡介
Apache CXF 是一個Service框架,他簡化了Service的建立, CXF實現了JAX-WS2.0規範,並經過了JAX-WS2.0 TCK; CXF和Spring無縫集成;CXF支持多種傳輸協議(HTTP, JMS, Corba等), 支持多種Binding數據格式(SOAP,XML,JSON等), 支持多種DataBinding數據類型(JAXB, Aegis) 。CXF基於Interceptor的架構,使得整個框架很是易於擴展。
二. 如何發佈並調用簡單的web service實例
2.1.下載:apache-cxf-2.1.1
http://cxf.apache.org/download.html
2.2. 新建java project ,並加入apache-cxf-2.0.7\lib全部包,編寫要發佈的web service 接口和實現
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
import javax.jws.WebService;
@WebService(endpointInterface="test.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String text) {
return "Hello" + text ;
}
}
@WebService 註解表示是要發佈的web 服務
name:用於Interface,屬映射到wsdl:portType element的name屬性。
targetNamespace:用於Interface和implement,若是不指定,缺省會使用包名倒序作爲wsdl名空間。
serviceName:用於implement,表示wsdl服務名。
portName:用於implement,表示wsdl:port 的name屬性。
endpointInterface:用於implement,指定Interface全名,包括包名。
2.3.發佈web service
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "
http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
運行後,在瀏覽器中輸入
http://localhost:9000/helloWorld?wsdl將顯示這個web service的wsdl.說明web service發佈成功。
2.4.下面就開始建立一個客戶端程序,訪問這個web service, 一樣新建java project ,並加入apache-cxf-2.0.7\lib全部包,因爲CXF已經提供wsdl轉化成java 的命令工具,因此建立一個build.xml,用來生成客戶端程序。Bulid.xml內容以下:
<?xml version="1.0"?>
<project name="cxf wsdl2java" basedir=".">
<property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/>
<path id="cxf.classpath">
<fileset dir="${cxf.home}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="cxfWSDLToJava">
<java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
<arg value="-client"/>
<arg value="-d"/>
<arg value="src"/>
<arg value="http://localhost:9000/helloWorld?wsdl"/>
<classpath>
<path refid="cxf.classpath"/>
</classpath>
</java>
</target>
</project>
或者:
配置環境變量%CXF_HOME%=E:\WebService\CXF\apache-cxf-2.1.1\apache-cxf-2.1.1(以個人目錄爲例),並在PATH後加上;%CXF_HOME%\bin在cmd命令行中輸入wsdl2java若是顯示其用法表示配置好了。
輸入:
wsdl2java -d src - client http://localhost:9000/helloWorld?wsdl 其做用上面的build.xml做用同樣。
附加:wsdl2java用法:
wsdl2java -p com -d src -all aa.wsdl
-p 指定其wsdl的命名空間,也就是要生成代碼的包名:
-d 指定要產生代碼所在目錄
-client 生成客戶端測試web service的代碼
-server 生成服務器啓動web service的代碼
-impl 生成web service的實現代碼
-ant 生成build.xml文件
-all 生成全部開始端點代碼:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
詳細用法見:
http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html
2.5.調用web service
public class MyClient {
public static void main(String[] argv) {
HelloWorld hello = new HelloWorldImplService().getHelloWorldImplPort();
System.out.println(hello.sayHello("Tom") );
}
}
注意:運行時,要必定先要發佈web sevice.
三. 參考資料
1.CXF 主頁:
http://cxf.apache.org/
2. CXF中文討論組:
http://groups.google.com/group/cxf-zh
3. Web service:
http://www.w3school.com.cn/webservices/index.asp
4. WSDL:
http://www.w3school.com.cn/wsdl/index.asp 5. SOAP:http://www.w3school.com.cn/soap/index.asp