用Axis2開發web service #1 - 從生成WSDL文件開始

- Contrast-First VS Code-First

Contract-first 開發方式是先定義XML Schema/WSDL契約,然後再寫代碼。
Code-first 開發方式是根據源代碼,對於java來講,通常是個interface,根據這個interface以及相關的依賴類,比如一些bean類來生成WSDL/XSD等。

- WSDL基礎知識

學習網站
http://www.w3schools.com/wsdl/
http://www.w3school.com.cn/wsdl/index.asp
http://www.w3school.com.cn/schema/index.asp

- Code-First Style to generate WSDL file

1. download axis2 binary distribution.

2. configure environment variables.

If your OS is window vista, control panel - system - advanced system settings - tab 'advanced' - button 'Environment Variables...'
setup the following variables:
AXIS2_HOME= the folder of axis2
PATH = %AXIS2_HOME%\bin

3. create a java project in eclipse, let's say project name testaxis1.

4. define an interface class,which exposes all methods to web service.

package com.test.axis.service;

import com.test.axis.bean.AuthUserReq;
import com.test.axis.bean.UserInfoResp;
import com.test.axis.bean.WebServiceFault;

public interface UserServices {
    UserInfoResp getUserInfo(String userId) throws WebServiceFault;

    UserInfoResp authUser(AuthUserReq authReq) throws WebServiceFault;
}

5. complete its dependency classes as follows.

// ===== UserInfoResp.java ====
package com.test.axis.bean;

import java.util.Date;

public class UserInfoResp {

    private String userName;
    // setter and getter methods
}

// ===== AuthUserReq.java====
package com.test.axis.bean;

public class AuthUserReq{

    private String userName;
    private String password;

    // setter and getter methods
}

// ==== WebServiceFault.java =====
package com.test.axis.bean;

public class WebServiceFault extends Exception {
    private String errCode;
    private String errMessage;

     // setter and getter methods
}

6. generate wsdl file with tools java2wsdl.bat

Java2WSDL Reference

 

go to %testaxis1_folder%\bin and run this command:
java2wsdl -cn com.test.axis.service.UserServices -o ..\resource -of UserServices.wsdl -tn http://axis.test.com/ws/service -stn http://axis.test.com/ws -dlb doc/lit

you will find the WSDL file at %testaxis1_folder%\resource\UserServices.wsdl

Take note

* the xml elements in red.
Axis2 can not detect the parameter name defined in java interface, but gives a parameter 'args0' instead. It is better to modify the wsdl file and make it meaningful.
so change
<xs:element minOccurs="0" name="args0" nillable="true" type="xs:string"/>

to

<xs:element minOccurs="0" name="userId" nillable="true" type="xs:string"/>
and change
<xs:element minOccurs="0" name="args0" nillable="true" type="ax21:AuthUserReq"/>

to

<xs:element minOccurs="0" name="authUserReq" nillable="true" type="ax21:AuthUserReq"/>

It is very troublesome to manually change the name like 'args0', how to do it better?

someone suggests to use -g while compiling the java code, please refer to
http://wso2.org/blog/sumedha/3727


It is not straightforward on my view.

Actually, just remember DO NOT generate wsdl file based on an interface, based on an implement class instead, then axis2 can detect the real parameter name.




* the xml elements in blue. 
By default, axis2 set minOccurs="0" and nillable="true" for all fields. If based on your business logic, userName is mandatory and can not be Null, change the definition
  <xs:element minOccurs="0" name="userName" nillable="true" type="xs:string"/>
to
  <xs:element minOccurs="1" maxOccurs="1" name="userName" nillable="false" type="xs:string"/>

* -dlb doc/lit

This is a very good article to introduce the style of wsdl
http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/


--- Reference URLs
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章