软件编程
位置:首页>> 软件编程>> java编程>> Java调用wsdl接口的两种方法(axis和wsimport)

Java调用wsdl接口的两种方法(axis和wsimport)

作者:文明冲浪  发布时间:2023-06-23 14:41:22 

标签: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 /><h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3><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 /><h3>获得国内手机号码归属地数据库信息</h3><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接口的两种方法(axis和wsimport)

 在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接口的两种方法(axis和wsimport)

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

Java调用wsdl接口的两种方法(axis和wsimport)

 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;
   }

}

来源:https://blog.csdn.net/rgrhiea/article/details/126968336

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com