Eclipse + Apache AXIS2 Tips

Version

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

(Win7)

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]);
......

xs:choice

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

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

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

Payload

印出 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:complexType>
    <xs:sequence>
      ......
    </xs:sequence>
   </xs:complexType>
</xs:element>
需改成
<xs:element name="obj" type="ns0:MyObj" minOccurs="0"/>
<xs:complexType name="MyObj">
  <xs:sequence>
    ......
  </xs:sequence>
</xs:complexType>

Reference

http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html

https://docs.oracle.com/cd/E21764_01/web.1111/e13734/clients.htm#WSADV584

http://www.ibm.com/support/knowledgecenter/SS8S5A_6.2.0/com.ibm.curam.content.doc/WebServices/c_WEBSERV_Connectors2CreatingClientInvokingWebService1.html

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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