网上的wsdl 说明示例:
有个项目上遇到解析出来都是带有前缀的,例如:
![](https://img-blog.csdnimg.cn/img_convert/1e4d7b1fce30daf6a778af59312894c5.gif)
![](https://img-blog.csdnimg.cn/img_convert/ccd5b230515b0174690ea93bd98dd5a1.gif)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.founder.com">
<soapenv:Header/>
<soapenv:Body>
<ser:funInterFaceMZBYJ>
<ser:in0>?</ser:in0>
</ser:funInterFaceMZBYJ>
</soapenv:Body>
</soapenv:Envelope>
之前一直用的hutool 调用 webservice,都比较好用,但是遇到这种奇怪的格式就一直报命名空间的错误。只有换种方式,网上搜了下,亲测可用。参考地址:(20条消息) Java调用WebService的几种方式_世界joker的博客-CSDN博客_java webservice
具体步骤如下
1、引用包
<!--spring web Service的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<!--spring web service wsdl包-->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<!-- axis 1.4 jar start -->
<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.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</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.4</version>
</dependency>
2、执行示例
![](https://img-blog.csdnimg.cn/img_convert/0490932bb6b0dca289f21e4922bcafa0.gif)
![](https://img-blog.csdnimg.cn/img_convert/d4d1617d45e567ed56cfd8f225b6f6e2.gif)
//wsdl地址
String endpoint ="http://100.100.100.63:8888/founderWebs/services/ICalculateServicePZH";
//命名空间
String namespace = "http://www.w3.org/2001/XMLSchema";
//服务名
String serviceName = "ICalculateServicePZH";
//方法名
String methodName = "funInterFaceMZBYJ";
//soapAction
String soapAction = "";
Service service = new Service();
Call call = (Call) service.createCall();
//设置响应超时
call.setTimeout(3000);
//设置地址
call.setTargetEndpointAddress(endpoint);
//设置方法名
call.setOperationName(new QName(namespace,methodName));
String param = "<funderService functionName='MZBYJ_201'>" +
" <value>1</value>" +
" <value>"+code+"</value> " +
" </funderService>";
//设置参数
call.addParameter("Message", XMLType.XSD_STRING, ParameterMode.IN);
String message = param;
Object [] obj = {message};
//设置返回类型
call.setReturnType(XMLType.XSD_STRING);
//启用soap
call.setUseSOAPAction(true);
//设置soapAction
call.setSOAPActionURI(soapAction);
//设置服务名
SOAPService soapService = new SOAPService();
soapService.setName(serviceName);
call.setSOAPService(soapService);
String ret = (String) call.invoke(obj);
3、多参数使用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
/**
* 调用WebService接口之前,方法参数及参数赋值
* 当前接口中参数类型仅限于字符串、整数、布尔,参数为空的情况为字符串类型
* @param call 接口访问请求
* @param keys 字段(方法参数)
* @param values 字段值(参数对应值)
*
*/
private static void params(Call call, Object[] keys, Object[] values) {
if(keys == null || values == null) return;
if(keys.length == 0 || values.length == 0) return;
if(keys.length != values.length){
throw new IllegalArgumentException("接口方法参数与参数值不匹配!");
}
for(int i = 0; i < values.length; i++){
String key = (String) keys[i]; //方法参数
Object value = values[i]; //参数值
if(value == null){
call.addParameter(key, XMLType.XSD_STRING, ParameterMode.IN);
} else if(value instanceof String){
call.addParameter(key, XMLType.XSD_STRING, ParameterMode.IN);
} else if(value instanceof Integer){
call.addParameter(key, XMLType.XSD_INTEGER, ParameterMode.IN);
} else if(value instanceof Boolean){
call.addParameter(key, XMLType.XSD_BOOLEAN, ParameterMode.IN);
}
}
}
4、多参数使用
String[] keys={"input1","input2"};
String[] values={input1,input2};
params(call, keys, values);
String ret = (String) call.invoke(values);