当前位置: 代码网 > it编程>编程语言>Java > Springboot使用CXF进行WSDL转换及WebService服务调用过程

Springboot使用CXF进行WSDL转换及WebService服务调用过程

2025年09月24日 Java 我要评论
在开发过程中,对接比较老的系统经常需要将 wsdl 文件转换为 java 代码以便与 web 服务进行交互。apache cxf 提供了便捷的方式来完成这一任务。本文将详细介绍如何使用 cxf 添加相

在开发过程中,对接比较老的系统经常需要将 wsdl 文件转换为 java 代码以便与 web 服务进行交互。apache cxf 提供了便捷的方式来完成这一任务。

本文将详细介绍如何使用 cxf 添加相关依赖,并通过两种方法将 wsdl 文件转换为 java 代码。最后,我们将展示如何调用生成的 webservice 服务接口。

1.添加依赖

<dependency>
    <groupid>org.apache.cxf</groupid>
    <artifactid>cxf-spring-boot-starter-jaxws</artifactid>
    <version>3.5.8</version>
</dependency>
<dependency>
    <groupid>org.apache.cxf</groupid>
    <artifactid>cxf-rt-transports-http</artifactid>
    <version>3.5.8</version>
</dependency>

2.wsdl转java代码,这有两种方式

第一种,使用bat命令转换

wsimport -keep http://10.1.1.1:8080/webservice/111/service/webservice?wsdl

第二种,使用maven插件实现(版本尽量和cxf依赖版本一致)

<plugin>
    <groupid>org.apache.cxf</groupid>
    <artifactid>cxf-codegen-plugin</artifactid>
    <version>3.5.8</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceroot>src/main/resources/cxf</sourceroot>
                <wsdloptions>
                    <wsdloption>
                        <wsdl>http://10.1.1.1:8080/webservice/111/service/webservice?wsdl</wsdl>
                    </wsdloption>
                </wsdloptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>

wsdl为这种格式,类似接口文档

转换为下图这种(其中get set都是方法)

3.这是转换后的webservice类

其中name是web service的本地名称,targetnamespace指定了web service的目标命名空间,wsdllocation指定了wsdl文档的位置,servicename是wsdl中定义的服务的名称,portname指定了wsdl中定义的端口的名称。

@webserviceclient(name = "webservice", targetnamespace = "http://service.xxx", wsdllocation = "http://10.1.1.1:8080/webservice/111/service/webservice?wsdl")
public class webservice
    extends service
{

    private final static url webservice_wsdl_location;
    private final static webserviceexception webservice_exception;
    private final static qname webservice_qname = new qname("http://service.xxx", "webservice");

    static {
        url url = null;
        webserviceexception e = null;
        try {
            url = new url("http://10.1.1.1:8080/webservice/111/service/webservice?wsdl");
        } catch (malformedurlexception ex) {
            e = new webserviceexception(ex);
        }
        webservice_wsdl_location = url;
        webservice_exception = e;
    }

    public webservice() {
        super(__getwsdllocation(), webservice_qname);
    }

4.我们需要使用webservice去调用getwebservicehttpport这个方法

找到要调用的方法名,把参数传进去(name,password,data),data是要传输的数据

webservice webservice = new webservice();
webserviceporttype port = webservice.getwebservicehttpport();
string result = port.sethtinfo(checcmdusername, checcmdpassword, data);
system.out.println("result: " + result);
if(result.contains("成功")){
    log.info("---------------同步成功!---------------");
    return true;
}else {
    log.info("---------------同步失败!---------------");
    return false;
}

通过上述步骤,我们可以轻松地将 wsdl 文件转换为 java 代码,并使用生成的类调用 web 服务接口。

无论是通过批处理命令还是使用 maven 插件,cxf 都提供了灵活且强大的工具来简化这一过程。

总结

希望本文能帮助你在实际项目中更高效地进行 web 服务开发和集成。

以上为个人经验,给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com