需要的jar
<dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.xml.rpc</groupId> <artifactId>javax.xml.rpc-api</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency>
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
依赖
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency>
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. 依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>4.3.2</version> </dependency>
3. 代码
public static void main(String[] args) { // 拼接xml soapui 导出 String orderSoapXml = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> " + " <soapenv:Header/> " + " <soapenv:Body> " + " <tem:HelloWorld/> " + " </soapenv:Body> " + "</soapenv:Envelope>"; // 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; }