网站首页 > 知识剖析 正文
将 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代码。
猜你喜欢
- 2025-08-05 Axis2 通过WSDL生成WebService客户端
- 2025-08-05 两分钟明白什么是 WSDL 和 UDDI(Web服务)
- 08-0612 个最佳 JavaScript 动画库,让您的 Web 页面动起来
- 08-06HTML 二次函数图像动画展示
- 08-06UnoCSS 内置的动画
- 08-06炫酷的CSS3 loading加载动画,总有一款适合你
- 08-06想要开发更好的Python项目,代码质量是关键
- 08-06想要入门学好Python编程,先从这几本书开始
- 08-06甲方VS程序员精彩画面鉴赏
- 08-06后端语言性能排行,哪种语言最快,为什么?
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)