Java调用wsdl接口的两种方法(axis和wsimport)
作者:文明冲浪 时间:2023-06-23 14:41:22
一、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地址>
在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文件拷贝至原先项目中
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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis源码解析——获取SqlSessionFactory方式
![](https://img.aspxhome.com/file/2023/2/62082_0s.png)
一篇文章带你入门Java变量
![](https://img.aspxhome.com/file/2023/8/61948_0s.jpg)
javaweb Servlet开发总结(二)
![](https://img.aspxhome.com/file/2023/7/58567_0s.png)
Java使用备忘录模式实现过关类游戏功能详解
![](https://img.aspxhome.com/file/2023/2/60782_0s.png)
利用spring的拦截器自定义缓存的实现实例代码
Linux下动静态库的打包与使用指南(C/C++)
![](https://img.aspxhome.com/file/2023/8/68778_0s.png)
Java 求解如何把二叉搜索树转换为累加树
![](https://img.aspxhome.com/file/2023/8/61018_0s.png)
java实现给图片加铺满的网格式文字水印
![](https://img.aspxhome.com/file/2023/9/57809_0s.jpg)
SpringBoot访问外部文件及默认路由问题
![](https://img.aspxhome.com/file/2023/7/65157_0s.png)
Spring定时任务使用及如何使用邮件监控服务器
Maven的几个常用plugin
IDEA编译报错:Error:java:无效的源发行版:17的解决办法
![](https://img.aspxhome.com/file/2023/3/62453_0s.png)
Java8新特性之空指针异常的克星Optional类的实现
java Springboot实现教务管理系统
![](https://img.aspxhome.com/file/2023/6/64906_0s.png)
IDEA使用GsonFormat完成JSON和JavaBean之间的转换
![](https://img.aspxhome.com/file/2023/8/61058_0s.png)
Java中Validated、Valid 、Validator区别详解
详解SpringCloud Ribbon 负载均衡通过服务器名无法连接的神坑
![](https://img.aspxhome.com/file/2023/7/61237_0s.png)
Android实现简易浏览器遇到问题的解决方法
java 实现发短信功能---腾讯云短信
![](https://img.aspxhome.com/file/2023/5/60765_0s.jpg)
java实现简单的webservice方式
![](https://img.aspxhome.com/file/2023/9/58989_0s.jpg)