Axis1.4调用.Net返回值为DataSet类型的WebService接口

Axis1.4调用.Net返回值为DataSet类型的WebService接口

  • 1.相关说明
  • 2.Axis1.4下载及依赖
    • 1.Axis1.4下载
    • 1.非maven环境导入依赖的包
    • 3.maven环境导入依赖的包
  • 3.WebService服务接口地址
  • 4.编写调用WebService服务的方法及数据解析

1.相关说明

  • 首先要说的是:在多种语言共存的编程环境下,是不适合使用类似DataSet这种只属于特定语言的数据类型的,应该尽量使用多语言都支持的简单数据类型以及简单数据类型的数组进行跨平台之间的接口调用。
  • 但往往有些时候就会碰到这种返回值为Dataset类型的WebService接口,而且暴露服务接口的一方不愿意做改动,这时候难题就丢给调用方了,这里使用Java语言对.Net返回值为DataSet类型的WebService接口进行调用与数据解析。
  1. JDK版本:1.8.0_172
  2. axis版本:Axis1.4

2.Axis1.4下载及依赖

1.Axis1.4下载

  1. 官网:http://axis.apache.org/axis/
  2. 下载官网上的bin压缩文件:axis-bin_1.4.zip

1.非maven环境导入依赖的包

导入axis-bin_1.4.zip包下的lib目录下的所有jar包,如下图:
Axis1.4调用.Net返回值为DataSet类型的WebService接口_第1张图片

3.maven环境导入依赖的包

在pom.xml中添加下面的依赖即可


        
        <dependency>
            <groupId>org.apache.axisgroupId>
            <artifactId>axisartifactId>
            <version>1.4version>
        dependency>
        
        <dependency>
            <groupId>axisgroupId>
            <artifactId>axis-jaxrpcartifactId>
            <version>1.4version>
        dependency>
        
        <dependency>
            <groupId>axisgroupId>
            <artifactId>axis-antartifactId>
            <version>1.4version>
        dependency>
        
        <dependency>
            <groupId>axisgroupId>
            <artifactId>axis-saajartifactId>
            <version>1.4version>
        dependency>
        
        <dependency>
            <groupId>wsdl4jgroupId>
            <artifactId>wsdl4jartifactId>
            <version>1.6.3version>
        dependency>
        
        <dependency>
            <groupId>commons-discoverygroupId>
            <artifactId>commons-discoveryartifactId>
            <version>0.5version>
        dependency>
        
		
 		
        <dependency>
            <groupId>org.dom4jgroupId>
            <artifactId>dom4jartifactId>
            <version>2.1.1version>
        dependency>

3.WebService服务接口地址

  • 这里使用一个网上找的一个使用.Net写的WebService接口作为参考实例进行解析
  1. WS接口地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
  2. 调用的方法:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportDataSet
  3. 调用getSupportDataSet方法后,wsdl文档如下图:
    Axis1.4调用.Net返回值为DataSet类型的WebService接口_第2张图片

4.编写调用WebService服务的方法及数据解析

编写调用WebService服务的客户端java类,并打印结果,类名为:Axis1_Client

package com.yuan;


import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.MessageElement;
import org.apache.axis.types.Schema;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

import javax.xml.namespace.QName;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: jinshengyuan
 * Date: 2019-01-15
 * Time: 15:13
 * description:
 **/
public class Axis1_Client {
    /**
     * 使用dom4j解析数据
     */
    @Test
    public void axisWSInvoke(){
        String dataSetDataStr = axisInvokeNetDataSetData();
        //System.out.println(dataSetDataStr);
        if(dataSetDataStr != null){
            try {
                Document doc = DocumentHelper.parseText(dataSetDataStr);// 将字符串转为XML
                Element root = doc.getRootElement();// 获取根节点
                Iterator iterator =  root.elementIterator("Zone");//迭代节点
                String id,zone;
                while(iterator.hasNext()){
                  Element element = (Element) iterator.next();
                  id = element.elementTextTrim("ID");//取出Zone节点下的ID元素的值
                  zone = element.elementTextTrim("Zone");//取出Zone节点下的Zone元素的值
                    System.out.println("Id:"+id+"=============================Zone:"+zone);
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 调用.Net写的返回值为DataSet类型的WebService服务
     * @return
     */
    public String axisInvokeNetDataSetData(){
        Service service = new Service();
        String strXml = null;
        Call call = null;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"));//WSURL,注意不要带?wsdl
            //调用方法方法前设置相关参数
            call.setOperationName(new QName("http://WebXml.com.cn/", "getSupportDataSet"));
            call.setReturnType(XMLType.XSD_SCHEMA);//返回类型,这里一定要传入 XMLType.XSD_SCHEMA 类型
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://WebXml.com.cn/getSupportDataSet");//soapAction
            Object obj = call.invoke((Object[]) null);
            Schema schema = (Schema) obj;
            MessageElement[] messageElements = schema.get_any();
            List messageHead = messageElements[0].getChildren();//消息头,DataSet对象
            List messageBody = messageElements[1].getChildren();//消息体信息,DataSet对象,最终需要解析的数据
            if (messageBody.size() > 0) {
                String head = messageHead.get(0).toString();//消息头,DataSet对象
                String diffgr = messageBody.get(0).toString();//消息体的字符串形式
                strXml = diffgr;
                System.out.println("head:\n"+head);
                System.out.println("diffgr:\n" + diffgr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strXml;
    }
}
  1. 输出结果:
Id:1=============================Zone:直辖市
Id:2=============================Zone:特别行政区
Id:3=============================Zone:黑龙江
Id:4=============================Zone:吉林
Id:5=============================Zone:辽宁
Id:6=============================Zone:内蒙古
Id:7=============================Zone:河北
Id:8=============================Zone:河南
Id:9=============================Zone:山东
Id:10=============================Zone:山西
Id:11=============================Zone:江苏
Id:12=============================Zone:安徽
Id:13=============================Zone:陕西
Id:14=============================Zone:宁夏
Id:15=============================Zone:甘肃
Id:16=============================Zone:青海
Id:17=============================Zone:湖北
Id:18=============================Zone:湖南
Id:19=============================Zone:浙江
Id:20=============================Zone:江西
Id:21=============================Zone:福建
Id:22=============================Zone:贵州
Id:23=============================Zone:四川
Id:24=============================Zone:广东
Id:25=============================Zone:广西
Id:26=============================Zone:云南
Id:27=============================Zone:海南
Id:28=============================Zone:新疆
Id:29=============================Zone:西藏
Id:30=============================Zone:台湾
Id:31=============================Zone:亚洲
Id:32=============================Zone:欧洲
Id:33=============================Zone:非洲
Id:34=============================Zone:北美洲
Id:35=============================Zone:南美洲
Id:36=============================Zone:大洋洲

你可能感兴趣的:(Web,Service)