Apache CXF将WSDL文件转换为java文件

Apache CXF将WSDL文件转换为java文件

编程文章jaq1232025-08-03 4:32:506A+A-

将 WSDL (Web Services Description Language) 文件转换为 Java 代码是开发 SOAP Web 服务客户端的常见需求。Apache CXF 是一个功能强大的开源 Web 服务框架,提供了 wsdl2java 工具用于从 WSDL 生成 Java 代码。

1、安装CXF

Apache CXF 官网 下载最新版本并解压。

Apache CXF -- Download

2、生成代码

在pom.xml中添加CXF依赖

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.5.5</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.5.5</version>
</dependency>

代码示例:

public static void main(String[] args) {
        // 指定要查询的文件夹路径
        String directoryPath = "D:\\nemanage\\wsdllocation";
        File directory = new File(directoryPath);
        if (directory.exists() && directory.isDirectory()) {
            // 开始递归查询并转换
            convertWsdlFilesInDirectory(directory);
        } else {
            System.err.println("指定的路径不是一个有效的文件夹。");
        }
    }

public static void convertWsdlFilesInDirectory(File directory) {
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        // 递归调用处理子文件夹
                        convertWsdlFilesInDirectory(file);
                    } else if (file.getName().endsWith("wsdl")) {
                        // 处理 WSDL 文件
                        convertWsdlToJava(file);
                    }
                }
            }
        }
    }

public static void convertWsdlToJava(File wsdlFile) {
        try {
            // 构建 wsdl2java 命令
            System.out.println(wsdlFile.getAbsolutePath());
            String path = wsdlFile.getAbsolutePath().substring(0, wsdlFile.getAbsolutePath().lastIndexOf("\\"));
            System.out.println(path);
            String file = wsdlFile.getAbsolutePath().substring(wsdlFile.getAbsolutePath().lastIndexOf("\\")+1,wsdlFile.getAbsolutePath().length());
            System.out.println(file);
          //根据自己需求选择合适的命令
            //String command = "wsdl2java -autoNameResolution -d " + "output" + " -p " + PACKAGE_NAME + "." + split[1]+ "." + split[4] + " "+wsdlFile.getAbsolutePath();
            //String command = "D: &&" + "cd "+ path + " && " + "wsdl2java -client -xjc-npa -autoNameResolution -d " + "D:\\nemanage\\xml\\output"  +" "+ file;
            //String command = "D: &&" + "cd "+ path + " && " + "wsdl2java -d " + "D:\\nemanage\\Wsdl2java"  +" "+ file;
            String command = "D: &&" + "cd "+ path + " && " + "wsdl2java -d " + "D:\\nemanage\\javaoutput"  +" "+ file;
            System.out.println(command);
//            String[] args = {
//                    "wsdl2java",
//                    "-d", OUTPUT_DIR,
//                    "-p", PACKAGE_NAME,
//                    "-autoNameResolution", // 自动解决命名冲突
//                    "-verbose", // 显示详细日志
//                    wsdlFile.getAbsolutePath()
//            };
            // 执行命令
            Process process = Runtime.getRuntime().exec("cmd /c"+ command);
            // 等待命令执行完成
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("change " + wsdlFile.getName() + " success");
            } else {
                System.err.println("change " + wsdlFile.getName() + " error: " + exitCode);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

执行上面代码,成功将wsdl文件转换为了java代码。

点击这里复制本文地址 以上内容由jaq123整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

苍茫编程网 © All Rights Reserved.  蜀ICP备2024111239号-21