Spring Boot 3中maven-jaxb2-plugin报错:javax.xml.bind.annotation不存在
我正在构建Spring Boot 3 SOAP消费者中间件,使用spring-boot-starter-web-services依赖和maven-jaxb2-plugin从WSDL生成STUB类,但构建项目时出现编译错误。
报错信息
[ERROR] /E:/java/client-2/src/main/java/com/soap/client/countrySOAP/CountryName.java:[15,33] package javax.xml.bind.annotation does not exist
问题原因
生成的STUB类导入了javax.xml.bind.annotation相关包,但Spring Boot 3基于Jakarta EE规范,依赖的是Jakarta XML Bind组件(对应包路径为jakarta.xml.bind.annotation),两者包路径不兼容导致编译失败。
当前项目pom.xml配置
<?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>3.0.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.soap</groupId> <artifactId>client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>4.0.1</version> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.14.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <generatePackage>com.soap.client.countrySOAP</generatePackage> <generateDirectory>${project.basedir}/src/main/java</generateDirectory> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <schemaIncludes> <include>*.wsdl</include> </schemaIncludes> </configuration> </plugin> </plugins> </build> </project>
生成的类代码片段
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType;
解决方案
1. 升级maven-jaxb2-plugin版本并配置Jakarta支持
旧版本的maven-jaxb2-plugin(如0.14.0)默认生成基于javax的注解,需要升级到支持Jakarta的版本(3.1.0及以上),并添加参数强制生成Jakarta风格的注解。
修改后的插件配置如下:
<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <!-- 升级到支持Jakarta的版本 --> <version>3.1.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <generatePackage>com.soap.client.countrySOAP</generatePackage> <generateDirectory>${project.basedir}/src/main/java</generateDirectory> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <schemaIncludes> <include>*.wsdl</include> </schemaIncludes> <!-- 添加参数生成Jakarta注解 --> <args> <arg>-jakarta</arg> </args> </configuration> </plugin>
2. 移除冗余依赖
spring-boot-starter-web-services已经包含了兼容Spring Boot 3的Jakarta XML Bind依赖,无需手动添加jakarta.xml.bind-api和jaxb-impl,移除这两个依赖避免版本冲突:
<!-- 移除以下两个依赖 --> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>4.0.1</version> <scope>runtime</scope> </dependency>
3. 重新生成代码
执行以下Maven命令清理并重新生成STUB类:
mvn clean generate-sources
此时生成的类会自动导入jakarta.xml.bind.annotation包,编译错误即可解决。
内容的提问来源于stack exchange,提问作者Rakib Ahmed

