Apache CXF将WSDL文件转换为java文件
将 WSDL (Web Services Description Language) 文件转换为 Java 代码是开发 SOAP Web 服务客户端的常见需求。Apache CXF 是一个功能强大的开源 Web 服务框架,提供了 wsdl2java 工具用于从 WSDL 生成 Java 代码。
1、安装CXF
从 Apache CXF 官网 下载最新版本并解压。
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代码。
相关文章
- java批量将word文档转换为pdf(javaopenofficepdf转word)
- Apache CXF将WSDL文件转换为java文件
- java实现文件上传到服务器(java上传文件到服务器,路径问题)
- 干掉 PowerDesigner!这款国人开源的数据库设计工具真香
- java+上传整个文件夹的所有文件(java文件上传方式)
- 推荐一个文件文档在线预览系统,号称Github最强
- Java-Maven详解(java中maven配置)
- Java更改 PDF 页面大小(java pdf怎么转换成word)
- 第1章 初识Java(初识java作业)
- 阿里云代理商:阿里云OSS文件上传Java实现教程