wsdl 接口调用

需要的jar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

依赖

1
2
3
4
5
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.4</version>
</dependency>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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. 依赖

1
2
3
4
5
6
7
8
9
10
<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. 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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/\">\n" +
            "   <soapenv:Header/>\n" +
            "   <soapenv:Body>\n" +
            "      <tem:HelloWorld/>\n" +
            "   </soapenv:Body>\n" +
            "</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;
}

  

 

 

posted @   qukaige  阅读(3873)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示