跳到主要内容Spring Boot 整合 WebService 客户端:基于 WSDL 自动生成代码 | 极客日志Javajava
Spring Boot 整合 WebService 客户端:基于 WSDL 自动生成代码
Spring Boot 项目集成 WebService 客户端时,可通过 WSDL 文件自动生成 Java 代码。主要流程包括引入 spring-boot-starter-ws 和 wsdl4j 依赖,获取服务端提供的 WSDL 文件,配置 Maven 的 jaxb2-plugin 插件指定生成包名与目录,最后执行 mvn install 命令完成代码生成。此方法避免了手动编写 SOAP 请求对象的繁琐工作,提升了开发效率。
蓝绿部署1 浏览 在 Spring Boot 项目中对接 WebService 服务时,利用 WSDL 文件自动生成客户端代码能显著减少重复劳动。下面以典型的 SOAP 服务为例,介绍如何从依赖引入到代码生成的完整流程。
引入必要依赖
首先需要确保项目中包含 WebService 支持及 WSDL 解析库。在 pom.xml 中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
获取 WSDL 文件
WebService 客户端的开发通常始于 WSDL 文件。你可以通过浏览器访问服务端地址并另存为,或者直接保留 URL 供后续使用。这里为了方便演示,我们假设已经获取了本地 WSDL 文件,其核心结构如下:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://www.dexcoder.com/ws" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.dexcoder.com/ws" targetNamespace="http://www.dexcoder.com/ws">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" =>
targetNamespace
"http://www.dexcoder.com/ws"
<xs:element name="getCountryRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getCountryResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="country" type="tns:country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="country">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="population" type="xs:int"/>
<xs:element name="capital" type="xs:string"/>
<xs:element name="currency" type="tns:currency"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="currency">
<xs:restriction base="xs:string">
<xs:enumeration value="GBP"/>
<xs:enumeration value="EUR"/>
<xs:enumeration value="PLN"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getCountryResponse">
<wsdl:part element="tns:getCountryResponse" name="getCountryResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getCountryRequest">
<wsdl:part element="tns:getCountryRequest" name="getCountryRequest">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="CountriesPort">
<wsdl:operation name="getCountry">
<wsdl:input message="tns:getCountryRequest" name="getCountryRequest">
</wsdl:input>
<wsdl:output message="tns:getCountryResponse" name="getCountryResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getCountry">
<soap:operation soapAction=""/>
<wsdl:input name="getCountryRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getCountryResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CountriesPortService">
<wsdl:port binding="tns:CountriesPortSoap11" name="CountriesPortSoap11">
<soap:address/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
配置 Maven 插件生成代码
与服务端通过 XSD 生成代码类似,客户端也可以借助 Maven 插件自动从 WSDL 生成 Java 类。我们需要引入 maven-jaxb2-plugin 并在 pom.xml 中配置生成规则:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>com.dexcoder.ws</generatePackage>
<generateDirectory>${basedir}/src/main/java</generateDirectory>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/resources/schemas</directory>
<includes>
<include>*.wsdl</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
</plugin>
注意将 WSDL 文件放入 src/main/resources/schemas 目录下,并确保文件名匹配。配置完成后,直接在项目根目录执行构建命令即可:
执行成功后,指定的 Java 源文件就会自动出现在 target/generated-sources 或配置的 generateDirectory 路径下,接下来就可以像使用普通 POJO 一样调用这些服务接口了。
相关免费在线工具
- Keycode 信息
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
- Escape 与 Native 编解码
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
- JavaScript / HTML 格式化
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
- JavaScript 压缩与混淆
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online