用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值