Eclipse + Apache AXIS2 Tips


JDK 1.6.0_45

AXIS2 1.6.2

Tomcat 6.0.32

Environment Setup

Step 1: Download Axis2 Binary Distribution and extract to a directory, such as C:\axis2-1.6.2.

Step 2: Download Tomcat and extract to a directory, such as C:\tomcat-6.0.32.

Step 3: Set Environment Variables


Open 電腦→內容→進階系統設定→環境變數→系統變數→Add JAVA_HOME=C:\Java\jdk1.6.0_45

Open 電腦→內容→進階系統設定→環境變數→系統變數→Add AXIS2_HOME=C:\axis2-1.6.2

Step 4: Set Eclipse Workspace Encoding.

Open Eclipse→Window→Preferences→General→Workspace→Text file encoding→UTF-8

Step 5: Set Eclipse Axis Runtime.

Open Eclipse→Window→Preferences→Web Services→Axis2 Preferences→Axis2 runtime location=指向Axis2目錄 (Step 1所建立的目錄)

Open Eclipse→Window→Preferences→Web Services→Server and Runtime→Web service runtime=Apache Axis2

Open Eclipse→Window→Preferences→Web Services→Scenario Defaults→Client generation level設到Assemble client就好

Step 6: Set Tomcat Runtime.

Open Eclipse→Window→Preferences→Server→Runtime Environments→Add Apache Tomcat→選取Tomcat installation directory=指向Tomcat目錄 (Step 2所建立的目錄)

WebService Client Project

Step 1. Open Eclipse→New→Java Project→Create a folder: wsdl→存放backend WSDL file

Step 2. Right click on a WSDL file→Web Services→Generate Client→Service runtime: Tomcat v6.0 Server; Web service runtime: Apache Axis2; Client project: 新增一WebApp (Dynamic Web Project) or 選一個既存的WebApp→Next→確認Port Name (如果有多個Port Name)→Finish. 基本上會建立出2個Class: XXXCallbackHandler & XXXSub

Step 3. Code snippet

String URL = "http://IP:Port/...";
XXXStub stub = new XXXStub(URL);
[response] resp = stub.operator([request]);


如果WSDL定義為 xs:choice,則 Axis2 會 generate 一層類似 ChoiceType 的物件,從中才可取得 choice 內的物件

<xs:choice minOccurs="0">
  <xs:element name="Obj1" type="ObjType1"/>
  <xs:element name="Obj2" type="ObjType2"/>

ChoiceType choiceType = myParentObj.getChoiceType();
if (choiceType.isObj1Specified) {
  ObjType1 obj1 = choiceType.getObj1();
} else if (choiceType.isObj2Specified) {
  ObjType2 obj2 = choiceType.getObj2();


印出 WebService 的 Request/Response Payload

logger.debug(doBooking.getOMElement(DoBooking.MY_QNAME, OMAbstractFactory.getOMFactory()).toStringWithConsume()); 

DoBookingResponse doBookingResponse = webServiceStub.doBooking(doBooking); 

logger.debug(doBookingResponse.getOMElement(DoBookingResponse.MY_QNAME, OMAbstractFactory.getOMFactory()).toStringWithConsume());

XSD Design Tips

如果定義element ref=”…” 另一個element,可能會造成 Axis2 無法成功 Gen Code, 會出現
ConverterUtil.convertToXXX is undefined for the type…的問題。需改成 element type=”…”才會正常Gen Code

<xs:element ref="ns0:MyObj" minOccurs="0"/> → Gen出的Code異常
<xs:element name="MyObj">
<xs:element name="obj" type="ns0:MyObj" minOccurs="0"/>
<xs:complexType name="MyObj">

出現 org.codehaus.stax2.typed.TypedXMLStreamException: ParseError at [row,col]:…
Message: Element content can not contain child START_ELEMENT when using Typed Access methods的錯誤

我覺得主要是Axis2並沒有完全支援WSDL的語法,有些定義方式是合法的,但Axis2可能無法Gen出正確的Code,有的即使正常Gen Code也可能在Runtime時出錯

本次出錯的原因是在WSDL中有兩個同名的complexType分屬於不同schema,雖然可Gen Code,但在執行時卻出現Exception。只要將其中一個沒使用的complexType刪除,再重新Gen Code,即可正常運作

<xs:schema xmlns:ns1=...>
  <xs:complexType name="MyInfo">
<xs:schema xmlns:ns2=...>
  <xs:complexType name="MyInfo"> → 把沒使用的刪除



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s