java利用axis2调用.net写的webservice,传递自定义的实体类参数

利用axis2可以很方便的自动生成客户端代码,同时对复杂参数类型的传递也很方便,本文的服务端以.net开发,有一个自定义的实体类作为参数,客户端用java,简单介绍一下利用axis2的wsdl2java自动生成客户端代码,并调用webservice的过程。

服务端说明:

服务URL:http://localhost:1057/AudioServ.asmx

默认命名空间:http://tempuri.org/

方法:AudioInfoCollection

参数是一个自定义的实体类,类名:ParameterClass,参数名:param

服务端xml如下:

POST /AudioServ.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/AudioInfoCollection"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AudioInfoCollection xmlns="http://tempuri.org/">
      <param>
        <CallSerialMain>string</CallSerialMain>
        <RecordLisQuestion>string</RecordLisQuestion>
        <StartDate>string</StartDate>
        <UserCityCode>string</UserCityCode>
        <UserCityName>string</UserCityName>
        <RecordLisStaffId>string</RecordLisStaffId>
        <RecordLisDate>string</RecordLisDate>
        <CallSerialNo>string</CallSerialNo>
        <UserCredit>string</UserCredit>
        <RecordLisContact>string</RecordLisContact>
        <RecordLisRemark>string</RecordLisRemark>
        <AudioFilePath>string</AudioFilePath>
      </param>
    </AudioInfoCollection>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AudioInfoCollectionResponse xmlns="http://tempuri.org/">
      <AudioInfoCollectionResult>boolean</AudioInfoCollectionResult>
    </AudioInfoCollectionResponse>
  </soap:Body>
</soap:Envelope>


返回bool类型


Axis2准备:

1、下载 Axis2-1.6.2(版本不固定,本文使用的是此版本),解压到相应目录,本文的目录统一为:D:\ JavaExternalLib\Axis2-1.6.2

2、配置环境变量,新增AXIS2_HOME,值为axis2的解压目录:D:\JavaExternalLib\Axis2-1.6.2

3、使用axis2的wsdl2java工具自动生成客户端代码

wsdl2java.bat工具在axis2安装目录下的bin目录中,D:\JavaExternalLib\Axis2-1.6.2\bin

打开Windows控制台(win+r 输入cmd),输入命令:

%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:1057/AudioServ.asmx?wsdl -p client -s -o client

-uri:指定wsdl文件的路径,本地路径或者网络路径都可以

-p:生成java类的包名

-o:生成文件的放置的相对目录

执行成功后显示:

Using AXIS2_HOME:   D:\JavaExternalLib\Axis2-1.6.2
Using JAVA_HOME:    C:\Program Files\Java\jdk1.8.0
Retrieving document at 'http://localhost:1057/AudioServ.asmx?wsdl'.
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.WSDL11ToAllAxisServicesBuilder).
log4j:WARN Please initialize the log4j system properly.


在控制台的当前目录下,生成了client目录,在client\src\client目录下,有一个AudioServStub.java文件,该文件即生成的客户端代码,负责调用webservice,可以直接使用。


调用:

eclipse新建一个java项目,包名client,将自动生成的客户端类AudioServStub.java添加到client包,为了省事,将axis2主目录下lib里面的所有jar包都应用到项目中, 新建类:CallWebService:

package client;  
  
public class CallWebService  
{         
    public static void main(String[] args)   
    {  
        try  
        {  
            System.out.println("Begin....");  
            callServ();           
            System.out.println("End...");          
        }  
        catch ( Exception ex)  
        {  
            System.err.println(ex.toString());  
        }                 
    }  
      
    public static void callServ()  
    {                        
        try  
        {  
            /* wsdl2java.bat命令生成的Stub类将WebService方法的参数都封装在了相应的类中, 
             * 类名为方法名 
             * */  
            AudioServStub.ParameterClass param = new AudioServStub.ParameterClass();      
              
			param.setCallSerialMain("122388048");  
            param.setRecordLisQuestion("01");                        
            param.setStartDate("20160425133100");                    
            param.setUserCityCode("123");                           
            param.setUserCityName("chengde");                           
            param.setRecordLisStaffId("AJF00033");                   
            param.setRecordLisDate("20160425133100");                
            param.setCallSerialNo("20160425133100001");              
            param.setUserCredit("sdsds");                             
            param.setRecordLisContact("01");                         
            param.setRecordLisRemark("test");                        
            param.setAudioFilePath("xxxxxx");  
              
            AudioServStub ass = new AudioServStub();  
            AudioServStub.AudioInfoCollection aaAudioInfoCollection = new AudioServStub.AudioInfoCollection();  
              
            //设置参数  
            aaAudioInfoCollection.setParam(param);  
              
            //调用  
            if(ass.audioInfoCollection(aaAudioInfoCollection).getAudioInfoCollectionResult())  
            {  
                System.out.println("Success...");  
            }  
            else   
            {  
                System.out.println("Error...");  
            }  
        }  
        catch (Exception ex)  
        {  
        	System.err.println(ex.toString());  
        }                 
    }         
}  


方法callServ完成调用


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值