版權聲明:轉載時請務必以超鏈接形式標明文章原文出處和作者信息
個人聲明:文章中的內(nèi)容如果引用了您的原創(chuàng),給您帶來了負面的影響,請聯(lián)系我
技術支持:如果有任何文章相關的技術問題,請聯(lián)系bldmickey@gmail.com
Eclipse Apache CXF 集成
詳細見《Web Service Eclipse STP 安裝.doc》文檔。安裝文檔中包括Eclipse STP,Apache CXF相關的Plugins。
詳細見《Web Service Eclipse STP 安裝.doc》文檔。安裝文檔中包括Apache CXF Runtime的安裝。
注意:如果安裝了Apache Axis2 Tools,注意將相關的Plugins刪除,主要包括Axis2_Codegen_Wizard_1.3.0和Axis2_Service_Archiver_1.3.0,否則CXF無法自動從SEI(Service Endpoint Interface) 接口創(chuàng)建WSDL文件。
注意:在啟動Eclipse之前,需要在eclipse.ini文件中添加相關選項:-Dorg.osgi.framework.bootdelegation=*原因請具體查看下面的鏈接:http://wiki.eclipse.org/Eclisep_3.3_runtime_options_for_STP.SC#bootdelegation_configuration。否則可能會出現(xiàn)無法自動創(chuàng)建WSDL文件現(xiàn)象。
注意:可以選擇SOAP1.2,不過WSDL自動生成的時候,缺省只生成soap12的binding,不包括SOAP1.1,對于這種情況可能會出現(xiàn)一個問題:如果用VC(例如:VS2005) Web引用的方式調(diào)用的話,會出WS-I兼容性問題,因為它不識別soap12的binding。
package org.eclipse.stp.example; public interface SayHi { public String SayHello(String value); } |
package org.eclipse.stp.example;
import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; import javax.jws.WebMethod;
public interface SayHi { @WebMethod(operationName="SayHello", exclude=false) @ResponseWrapper(className="org.eclipse.stp.example.SayHelloResponse", localName="SayHelloResponse", targetNamespace="http://example.stp.eclipse.org/") @RequestWrapper(className="org.eclipse.stp.example.SayHello", localName="SayHello", targetNamespace="http://example.stp.eclipse.org/") public String SayHello(String value); } |
注意:上面定義了OperationName是:SayHello;同時定義了兩個類:SayHelloResponse和SayHello
package org.eclipse.stp.example;
import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; import javax.jws.WebMethod; import javax.jws.WebService;
@WebService(name="SayHi", targetNamespace="http://example.stp.eclipse.org/") public interface SayHi { @WebMethod(operationName="SayHello", exclude=false) @ResponseWrapper(className="org.eclipse.stp.example.SayHelloResponse", localName="SayHelloResponse", targetNamespace="http://example.stp.eclipse.org/") @RequestWrapper(className="org.eclipse.stp.example.SayHello", localName="SayHello", targetNamespace="http://example.stp.eclipse.org/") public String SayHello(String value); } |
注意:上面定義的Service的名字是SayHi.
注意:SOAPBinding中RPC模式不支持RequestWrapper方式
l 系統(tǒng)自動添加下面的代碼
package org.eclipse.stp.example;
import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding;
@SOAPBinding(use=SOAPBinding.Use.LITERAL, style=SOAPBinding.Style.DOCUMENT) @WebService(name="SayHi", targetNamespace="http://example.stp.eclipse.org/") public interface SayHi { @WebMethod(operationName="SayHello", exclude=false) @ResponseWrapper(className="org.eclipse.stp.example.SayHelloResponse", localName="SayHelloResponse", targetNamespace="http://example.stp.eclipse.org/") @RequestWrapper(className="org.eclipse.stp.example.SayHello", localName="SayHello", targetNamespace="http://example.stp.eclipse.org/") public String SayHello(String value); }
|
l 保存項目,系統(tǒng)將自動產(chǎn)生SayHi.wsdl文件(注意:正常的情況肯定會生成這個文件),自動產(chǎn)生的文件:SayHello.java和SayHelloReponse.java。
注意:WSDL文件中相關的關于soap:address的部分, <soap:address location="http://localhost:9090/hello"/>,這里的9090端口和hello只是作用在eclipse的客戶端服務器方式測試時會用到。如果發(fā)布到tomcat服務器,相關的soap:address會自動改變。
l 產(chǎn)生的WSDL的文件如下:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions name="SayHiService" targetNamespace="http://example.stp.eclipse.org/" xmlns:ns1="http://example.stp.eclipse.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.stp.eclipse.org/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://example.stp.eclipse.org/"> <xs:element name="SayHello" type="tns:SayHello"/> <xs:element name="SayHelloResponse" type="tns:SayHelloResponse"/> <xs:complexType name="SayHello"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="SayHelloResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="SayHelloResponse"> <wsdl:part name="result" element="ns1:SayHelloResponse"> </wsdl:part> </wsdl:message> <wsdl:message name="SayHello"> <wsdl:part name="parameters" element="ns1:SayHello"> </wsdl:part> </wsdl:message> <wsdl:portType name="SayHi"> <wsdl:operation name="SayHello"> <wsdl:input name="SayHello" message="ns1:SayHello"> </wsdl:input> <wsdl:output name="SayHelloResponse" message="ns1:SayHelloResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="SayHiServiceSoapBinding" type="ns1:SayHi"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="SayHello"> <soap:operation soapAction="" style="document"/> <wsdl:input name="SayHello"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="SayHelloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="SayHiService"> <wsdl:port name="SayHiPort" binding="ns1:SayHiServiceSoapBinding"> <soap:address location="http://localhost:9090/hello"/> </wsdl:port> </wsdl:service> </wsdl:definitions> |
l 選擇SayHi.wsdl文件->菜單選擇SOA->JAX-WS->Generate Code,顯示界面如下:
l 選擇Finish,代碼架構如下:
l 創(chuàng)建的Java程序:ObjectFactory.java, Package-info.java, SayHi_SayHiPort_Client.java, SayHi_SayHiPort_Server.java, SayHiImpl.java, SayHiService.java文件
l 修改SayHiImpl.java文件中的實現(xiàn)部分(下面藍色部分),返回“"Hello: " + arg0”
package org.eclipse.stp.example; import java.util.logging.Logger; public class SayHiImpl implements SayHi { private static final Logger LOG = Logger.getLogger(SayHiImpl.class.getName()); public java.lang.String sayHello(java.lang.String arg0) { LOG.info("Executing operation sayHello"); System.out.println(arg0); try { //java.lang.String _return = ""; java.lang.String _return = "Hello: " + arg0; return _return; } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } |
l SayHi_SayHiPort_Server.java相關代碼,下面藍色部分根據(jù)SayHi.wsld文件中的:<soap:address location="http://localhost::9090/hello"/>確定,此地址和Eclipse中測試Server啟動端口和URI相關。
package org.eclipse.stp.example; import javax.xml.ws.Endpoint; public class SayHi_SayHiPort_Server{ protected SayHi_SayHiPort_Server() throws Exception { System.out.println("Starting Server"); Object implementor = new SayHiImpl(); String address = "http://localhost:9090/hello"; Endpoint.publish(address, implementor); } public static void main(String args[]) throws Exception { new SayHi_SayHiPort_Server(); System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000); System.out.println("Server exitting"); System.exit(0); } } |
修改SayHi_SayHiPort_Client.java代碼中,關于客戶端訪問的代碼
package org.eclipse.stp.example; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; public final class SayHi_SayHiPort_Client { private static final QName SERVICE_NAME = new QName("http://example.stp.eclipse.org/", "SayHiService"); private SayHi_SayHiPort_Client() { } public static void main(String args[]) throws Exception { if (args.length == 0) { System.out.println("please specify wsdl"); System.exit(1); } URL wsdlURL = null; File wsdlFile = new File(args[0]); try { if (wsdlFile.exists()) { wsdlURL = wsdlFile. } else { wsdlURL = new URL(args[0]); } } catch (MalformedURLException e) { e.printStackTrace(); } SayHiService ss = new SayHiService(wsdlURL, SERVICE_NAME); SayHi port = ss.getSayHiPort(); System.out.println("Invoking sayHello..."); System.out.println("Invoking sayHello..."); //*************Changed by Zhenghao*************** //java.lang.String _sayHi_arg0 = ""; java.lang.String _sayHello_arg0 = "Zhenghao"; //*************Ended Change by Zhenghao********** java.lang.String _sayHello__return = port.SayHello(_sayHello_arg0); System.out.println("sayHello.result=" + _sayHello__return); System.exit(0); } } |
在生產(chǎn)環(huán)境中,將CXF下載軟件lib和modules目錄下的所有jar包拷貝到tomcat安裝目錄的shared/lib下面。重啟Tomcat。
l 菜單選擇File->New->Other…->選擇Server->Server后選擇Next
l 在Server’s Host name中缺省為localhost,選擇Apache->Tomcat v5.5 Server后選擇Next
l Tomcat Server設置中選擇Name:缺省,設置Tomcat installation directory中設置tomcat安裝的目錄,設置缺省的JRE版本,選擇Finish完成
l 菜單選擇Window->Show View->Other…->選擇Server->Server,在Servers的視圖中將現(xiàn)在Tomcat v5.5. Server at localhost的狀態(tài)是Stopped。雙擊Server想顯示Tomcat Server Overview的窗口。
l 在Server Location中選擇:Use Tomcat installation(take control of Tomcat installation)
l 工具欄中選擇保存
選中本地的tomcat服務器,右鍵選擇start,服務器的狀態(tài)從stopped到started
l 菜單選擇File->New->Other…->選擇SOA Tools->Deployment Profile后選擇Next;
l 在scxstp01中點擊wsdl,在File Name中輸入:SayHiDeploy,如下圖:選擇Next;
l 在Deployment Description選擇繼續(xù);Package中選擇繼續(xù);Target Server選擇繼續(xù);Summary中選擇Finish
l 在wsdl目錄下將創(chuàng)建SayHiDeploy.deploy項,右邊顯示deploy文件,在文件顯示框中選擇Configuration Tab
l 選擇Add Target…->在彈出對話框中選擇剛才啟動的“Tomcat v5.5 Server at localhost”,選擇OK,在Server框中將顯示剛才添加的target server。
l 選擇Create Target
l 選擇Deploy Package
l 查看Console Tab,將顯示Package Deploy的狀態(tài)(類似下面的log)
INFO: Creating Service {http://example.stp.eclipse.org/}SayHiService from WSDL: WEB-INF/wsdl/SayHi.wsdl
在瀏覽器中輸入:http://localhost:8081/SayHi/services/SayHi?wsdl
Deploy后的一個需要注意的地方,binding的地址和名稱根據(jù)
<wsdl:service name="SayHiService"> - <wsdl:port binding="ns1:SayHiServiceSoapBinding" name="SayHiPort"> <soap:address location="http://localhost:8081/SayHi/services/SayHi" /> </wsdl:port> </wsdl:service> |
|
在瀏覽器中輸入:http://localhost:8081/SayHi/services/SayHi/SayHello?arg0=bldmickey
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> - <soap:Body> - <ns2:SayHelloResponse xmlns:ns2="http://example.stp.eclipse.org/"> <return>Hello: bldmickey</return> </ns2:SayHelloResponse> </soap:Body> </soap:Envelope>
|
其中SayHello對應的是相應的Operation,其中arg0是參數(shù)名稱
l 菜單選擇Run->Open Run Dialog…->選擇Java Application -> SayHi_SayHiPort_Server->選擇Run。Console中顯示:
Aug 22, 2007 11:15:36 PM sun.reflect.NativeMethodAccessorImpl invoke0 INFO: Started SelectChannelConnector @ 0.0.0.0:9090 Server ready... |
l 菜單選擇Run->Open Run Dialog…->選擇Java Application -> SayHiClient…->選擇Run。Console中顯示:
INFO: Creating Service {http://example.stp.eclipse.org/}SayHiService from WSDL: file:/D:/2007/CodeWorm/WebService/SourceCode/CXF/scxstp01/wsdl/SayHi.wsdl Invoking sayHello... sayHello.result=Hello: Zhenghao |
l 或者在瀏覽器中測試
http://localhost:9090/hello?wsdl
結果基本和步驟十四相同,區(qū)別在于
- <wsdl:service name="SayHiService"> - <wsdl:port binding="ns1:SayHiServiceSoapBinding" name="SayHiPort"> <soap:address location="http://localhost:9090/hello" /> </wsdl:port> </wsdl:service> |
http://localhost:9090/hello/SayHello?arg0=zhenghao
結果基本和步驟十四相同
將SayHi.war文件直接復制到生產(chǎn)服務器上的:tomcat安裝目錄下的webapps子目錄下。修改文件的屬性。例如:
修改文件的屬主。su成tomcat用戶,touch SayHi.war。tomcat將自動生成目錄SayHi
Web瀏覽器測試:
http://ServerIP:Port/SayHi/services/SayHi?wsdl
- <wsdl:service name="SayHiService"> - <wsdl:port binding="ns1:SayHiServiceSoapBinding" name="SayHiPort"> <soap:address location="http://ServerIP:Port/SayHi/services/SayHi" /> </wsdl:port> </wsdl:service> |
注意:soap:address將會變成Server的IP地址
http://ServerIP:port/SayHi/services/SayHi/SayHello?arg0=bldmickey
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> - <soap:Body> - <ns2:SayHelloResponse xmlns:ns2="http://example.stp.eclipse.org/"> <return>Hello: bldmickey</return> </ns2:SayHelloResponse> </soap:Body> </soap:Envelope> |
聯(lián)系客服