Monday, January 3, 2011

Develop Web Service With Axis2 #1 - Start From Generating WSDL File

- Contrast-First VS Code-First

Contract-first development style is to develop web services that start with the XML Schema/WSDL contract first followed by the Java code second.
Code-first development style is to generate web service contract,WSDL and XSD, based on the java code.

- WSDL Knowledge

learn it from
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
http://www.keith-chapman.org/2008/10/axis2-wsdl2java-generate-better-code.html


No comments:

Post a Comment