温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java调用wsdl接口的方法有哪些

发布时间:2023-03-29 10:19:17 来源:亿速云 阅读:124 作者:iii 栏目:开发技术

本篇内容介绍了“Java调用wsdl接口的方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、AXIS调用远程WebService,以国内手机号归属地查询为例 

1、wsdl地址:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

2、导入依赖:

使用axis远程调用webService需要使用到axis、jaxrpc-api、commons-logging、commons-discovery等jar包。方便起见可以新建maven项目,在pom中导入依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.5</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>

3、调用有入参的webservice接口

阅读wsdl文件,我们可以了解方法名、参数和返回类型

<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h4>获得国内手机号码归属地省份、地区和手机卡类型信息</h4><p>输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoSoapIn"/>
<wsdl:output message="tns:getMobileCodeInfoSoapOut"/>
</wsdl:operation>

方法名为:getMobileCodeInfo

参数有两个:mobileCode手机号码,字符串类型;userID用户id,字符串类型(可以为空)

返回类型为字符串

Java调用代码

public static void getMobileCodeInfo() throws ServiceException, RemoteException {
    Service service = new Service();
    Call call = (Call) service.createCall();
    // wsdl完整地址
    call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
    /**
     * 设置方法名
     * new QName(String namespaceURI, String localPart) namespaceURI即为wsdl中的targetNamespace, localPart即为接口名
     */
    call.setOperationName(new QName("http://WebXml.com.cn/", "getMobileCodeInfo"));
    /**
     * 添加参数
     * addParameter方法的参数包括:参数名(namespace+参数名)、参数类型、ParameterMode(入参即为IN)
     */
    call.addParameter(new QName("http://WebXml.com.cn/", "mobileCode"), XMLType.XSD_STRING, ParameterMode.IN);
    call.setUseSOAPAction(true);
    // SOAPActionURI格式为targetNamespace+方法名
    call.setSOAPActionURI("http://WebXml.com.cn/getMobileCodeInfo");
    // 指定返回值类型,为字符串
    call.setReturnType(XMLType.XSD_STRING);
    call.setReturnClass(java.lang.String.class);
    String result = (String) call.invoke(new Object[]{"手机号码"});
    System.out.println(result);
}

4、调用无参的webservice接口

调用无参的webservice接口无需添加参数,并且在invoke方法中传入的是一个空的对象数组

T result = (T)call.invoke(new Object[]{});

 阅读wsdl文件,了解方法名、参数和返回类型

<wsdl:operation name="getDatabaseInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h4>获得国内手机号码归属地数据库信息</h4><p>输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getDatabaseInfoSoapIn"/>
<wsdl:output message="tns:getDatabaseInfoSoapOut"/>
</wsdl:operation>

方法名:getDatabaseInfo,参数:无,返回类型:一维字符串数组

Java调用代码

public static void getDatabaseInfo() throws ServiceException, RemoteException {
    Service service = new Service();
    Call call = (Call) service.createCall();
    call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");   //?wsdl
    call.setOperationName(new QName("http://WebXml.com.cn/", "getDatabaseInfo"));
    call.setUseSOAPAction(true);
    call.setSOAPActionURI("http://WebXml.com.cn/getDatabaseInfo");
    call.setReturnType(XMLType.XSD_UNSIGNEDBYTE);
    call.setReturnClass(java.lang.String[].class);
    String[] returnContext = (String[]) call.invoke(new Object[]{});
    for (String s : returnContext) {
        System.out.println(s);
    }
}

二、使用wsimport方法将wsdl转换为Java接口

wsimport命令是JDK自带的命令,它能够根据服务端说明书(wsdl)生成对应的本地java代码。这种方法相较于第一种要简单很多,不用阅读wsdl文件。

wsimport -d <生成.class文件的目录> -s <生成.java文件的目录> -p<包名> <wsdl地址>

Java调用wsdl接口的方法有哪些

 在D:\wsdl下新建文件夹class用于存放.class文件,文件夹java用于存放.java文件

D:\wsdl>wsimport -d class -s java -p mobileCode http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

执行命令,生成java代码 

Java调用wsdl接口的方法有哪些

 将Java文件拷贝至原先项目中

Java调用wsdl接口的方法有哪些

 Java代码示例

import mobileCode.MobileCodeWS;
import mobileCode.MobileCodeWSSoap;
 
import java.util.List;
 
public class Main {
    public static void main(String[] args) {
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        MobileCodeWSSoap soap = mobileCodeWS.getMobileCodeWSSoap();
        String mobileCodeInfo = soap.getMobileCodeInfo("手机号码", null);
        System.out.println(mobileCodeInfo);
        List<String> dbInfo = soap.getDatabaseInfo().getString();
        System.out.println(dbInfo);
    }
}

wsimport生成的Java代码中自定义了一个ArrayOfString数据结构,用于接收webservice返回的字符串数组,用getString()方法可以将之转化为列表

package mobileCode;
 
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
 
 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfString", propOrder = {
    "string"
})
public class ArrayOfString {
 
    @XmlElement(nillable = true)
    protected List<String> string;
 
    public List<String> getString() {
        if (string == null) {
            string = new ArrayList<String>();
        }
        return this.string;
    }
 
}

“Java调用wsdl接口的方法有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI