使用cxf调用webservice

本文介绍如何使用Apache CXF库创建SOAP Web服务客户端。通过Maven引入必要的依赖包,并提供创建动态客户端的方法及调用远程服务的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、引入maven包

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
View Code

2、创建Client对象

public static Client getDynamicClient(String wsdlUrl) {
        if (client == null) {
            synchronized (this.class) {
                if (client == null) {
                    DynamicClientFactory dcf = DynamicClientFactory.newInstance();
                    client = dcf.createClient(wsdlUrl);
                }
            }
        }
        return client;
    }
View Code

3、请求远程方法

    private String getResult(String reqXml, String wsdlUrl, String method) {
        String result = null;
        Object[] objects = null;
        try {
            LOGGER.debug("reqXml={}.", reqXml);
            objects = getDynamicClient(wsdlUrl).invoke(method, reqXml);
            result = (String) objects[0];
            LOGGER.debug("GbotExecutor.getResult response={}.", result);
        }
        catch (Exception e) {
            LOGGER.error("invoke gbot getResult failed!", e);
        }
        return result;
    }
View Code

 

转载于:https://www.cnblogs.com/BensonHe/p/5612117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值