
在spring boot项目中集成第三方soap web服务时,通常会收到一个wsdl url或文件。wsdl是一种xml格式的语言,用于描述web服务的接口、操作、数据类型和网络位置。为了在java应用程序中调用这些服务,我们需要将wsdl中定义的复杂类型和操作转换为相应的java类。这些生成的java类充当了服务契约的本地表示,允许开发者以面向对象的方式调用远程soap服务,而无需直接处理底层的xml消息。
在WSDL到Java类的转换过程中,开发者可能会遇到一些常见问题。
wsimport是JDK自带的一个用于生成JAX-WS(Java API for XML Web Services)客户端代码的命令行工具。然而,自Java 11起,JAXB(Java Architecture for XML Binding)模块(java.xml.bind)已从JDK中移除,成为一个独立的库。这意味着,如果您使用Java 11或更高版本运行wsimport,可能会遇到“Unable to locate a Java Runtime that supports wsimport”或类似的错误。
解决方案:
某些集成开发环境(IDE),如Eclipse,提供了内置的Web服务工具来生成Java客户端。但有时这些选项可能缺失,即使安装了“Enterprise Java and Web Developers”等特定软件包也可能不显示。
立即学习“Java免费学习笔记(深入)”;
解决方案:
将WSDL到Java的生成过程集成到Maven或Gradle构建生命周期中是最佳实践。这不仅解决了上述兼容性和IDE问题,还确保了代码生成的自动化和可重复性。
以Maven为例,我们可以使用jaxws-maven-plugin来处理WSDL文件并生成Java类。
将WSDL文件放入项目: 建议将WSDL文件放置在项目的特定目录下,例如src/main/resources/wsdl/。这样做有助于版本控制,并确保WSDL文件的可追溯性。
src/main/resources/wsdl/yourService.wsdl
配置pom.xml: 在项目的pom.xml文件中,添加jaxws-maven-plugin的配置。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version> <!-- 或您使用的Spring Boot版本 -->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>soap-client-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>soap-client-demo</name>
<description>Demo project for Spring Boot SOAP client</description>
<properties>
<java.version>17</java.version> <!-- 根据您的JDK版本设置 -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.5</version> <!-- 确保与您的JDK版本兼容的JAX-WS运行时 -->
</dependency>
<!-- Java 11+ 需要显式添加JAXB API和运行时 -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.5</version>
<scope>runtime</scope>
</dependency>
<!-- 其他Spring Boot依赖 -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3.2</version> <!-- 选择一个合适的版本 -->
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>yourService.wsdl</wsdlFile>
</wsdlFiles>
<packageName>com.example.soap.client</packageName> <!-- 生成类的包名 -->
<sourceDestDir>${project.build.directory}/generated-sources/jaxws</sourceDestDir>
<keep>true</keep> <!-- 保留生成的源文件 -->
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project>执行代码生成: 在项目根目录运行Maven命令:
mvn clean install
或者只生成源文件:
mvn generate-sources
执行后,生成的Java类将位于target/generated-sources/jaxws目录下(根据sourceDestDir配置),并被自动添加到项目的编译路径中。
对于Gradle项目,可以使用com.github.bjornvester.wsdl2java或gradle-jaxb-plugin等插件实现类似的功能。核心思想是在build.gradle中配置插件,指定WSDL源和输出目录,然后在构建任务中执行代码生成。
一旦WSDL转换为Java类,我们就可以利用Spring Boot的WebServiceGatewaySupport来构建SOAP客户端。
首先,创建一个配置类来定义WebServiceTemplate bean。
package com.example.soap.config;
import com.example.soap.client.YourServiceClient; // 假设这是生成的客户端接口或类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.support.WebServiceGatewaySupport;
@Configuration
public class SoapClientConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// 设置生成的Java类的包名
marshaller.setContextPath("com.example.soap.client"); // 与jaxws-maven-plugin配置的packageName一致
return marshaller;
}
@Bean
public YourServiceClient yourServiceClient(Jaxb2Marshaller marshaller) {
YourServiceClient client = new YourServiceClient();
client.setDefaultUri("http://compA.com/ws/server"); // 第三方SOAP服务的实际地址
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}创建一个继承自WebServiceGatewaySupport的客户端类。
package com.example.soap.client;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
// 假设WSDL生成了一个名为 'GetYourDataRequest' 和 'GetYourDataResponse' 的类
// 并且有一个名为 'YourServicePort' 的接口或类定义了服务操作
import com.example.soap.client.GetYourDataRequest;
import com.example.soap.client.GetYourDataResponse;
public class YourServiceClient extends WebServiceGatewaySupport {
public GetYourDataResponse getYourData(String inputParameter) {
GetYourDataRequest request = new GetYourDataRequest();
request.setInputField(inputParameter); // 根据实际生成的类设置请求参数
// 调用WebServiceTemplate发送请求
// "http://compA.com/ws/server/GetYourData" 替换为实际的SOAP Action URI
// 或根据WSDL中的定义
GetYourDataResponse response = (GetYourDataResponse) getWebServiceTemplate()
.marshalSendAndReceive(
getDefaultUri(),
request,
new SoapActionCallback("http://compA.com/ws/server/GetYourData"));
return response;
}
// 可以添加更多服务方法...
}在您的Spring Boot组件(如Controller或Service)中注入并使用YourServiceClient。
package com.example.soap.service;
import com.example.soap.client.GetYourDataResponse;
import com.example.soap.client.YourServiceClient;
import org.springframework.stereotype.Service;
@Service
public class MyApplicationService {
private final YourServiceClient yourServiceClient;
public MyApplicationService(YourServiceClient yourServiceClient) {
this.yourServiceClient = yourServiceClient;
}
public String fetchDataFromSoapService(String query) {
GetYourDataResponse response = yourServiceClient.getYourData(query);
// 处理响应数据
return response.getOutputField(); // 假设响应有一个outputField
}
}将WSDL转换为Java类是Spring Boot项目集成SOAP Web服务的关键一步。通过采用Maven或Gradle等构建工具,我们可以克服wsimport兼容性及IDE工具缺失等常见挑战,实现WSDL到Java代码生成的自动化和标准化。结合Spring Boot的WebServiceGatewaySupport,开发者可以高效、可靠地构建和消费SOAP服务,从而顺利地与外部系统进行集成。遵循上述最佳实践,将确保您的SOAP客户端代码健壮、可维护且易于管理。
以上就是高效集成SOAP服务:Spring Boot中WSDL转Java的实践与策略的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号