wsdl 接口调用
需要的jar
org.apache.axis axis 1.4 javax.xml.rpc javax.xml.rpc-api 1.1.2 commons-discovery commons-discovery 0.2 wsdl4j wsdl4j 1.6.2
java 代码
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
public static void main(String[] args) {
try {
String url = "http://localhost:9000/HelloWorld?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName("http://example/","sayHelloWorldFrom"));//命名空间url 和方法名字
// call.addParameter("from", org.apache.axis.encoding.XMLType.XSD_STRING,
// javax.xml.rpc.ParameterMode.IN);//接口的参数
// 参数名, 参数类型String, IN 或 OUT
call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN); // 这里参数不能写参数名, arg0 代表第一个参数
call.setReturnType(XMLType.XSD_STRING);//设置返回类型
String result = (String)call.invoke(new Object[]{"xxx"});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
方式 2 springboot
依赖
org.apache.cxf cxf-spring-boot-starter-jaxws 3.2.4
private static void test() {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:9000/HelloWorld?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
try {
// invoke("方法名",参数1,参数2,参数3....);
Object[] helloWorlds = client.invoke("sayHelloWorldFrom", "xxx");
for (Object o : helloWorlds) {
System.out.println(o);
}
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
方式3 : httpClient 方式
1. 安装SoapUi 获取 xml
https://blog.csdn.net/w0002399/article/details/82051404
2. 依赖
org.apache.httpcomponents httpclient 4.3.2 org.apache.httpcomponents fluent-hc 4.3.2
3. 代码
public static void main(String[] args) {
// 拼接xml soapui 导出
String orderSoapXml = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ";
// url
String url = "http://localhost:9000/HelloWorld/?wsdl";
//采用SOAP1.1调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务
doPost(url, orderSoapXml, "");
}
public static String doPost(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000)
.setConnectTimeout(30000).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应xml内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
log.info("response:" + retStr);
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
log.error("exception in doPostSoap1_1", e);
}
return retStr;
}