You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.7迁移至3.1.2遇javax.xml.bind.DatatypeConverter缺失问题咨询

Spring Boot 3.1.2迁移后JWT生成报javax.xml.bind.DatatypeConverter找不到的问题解决建议

问题描述

将基于Spring Boot 2.7的应用迁移至Spring Boot 3.1.2,完成安全配置后可正常构建运行,但使用Bearer token登录时触发java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter错误,Spring Boot 2.7版本无此问题。报错相关代码如下:

public String generateAccessToken(User user) {
    List<String> roleNames = user.getRoles().stream()
        .map(Role::getRoleType)
        .map(Enum::name)
        .toList();

    return Jwts.builder()
        .setSubject(String.format("%s,%s", user.getId(), user.getEmail()))
        .claim("roles", roleNames)
        .setIssuedAt(new Date())
        .setExpiration(new Date(System.currentTimeMillis() + jwtExpireDate))
        .signWith(SignatureAlgorithm.HS512, jwtSecret)
        .compact();
}

原因分析

Spring Boot 3.x基于Jakarta EE 9规范,所有Java EE时代的javax.*包已被替换为jakarta.*包;同时JAXB API在Java 9之后被移出JDK核心库,Spring Boot 3默认不再包含相关依赖。而旧版本的JJWT(JWT生成库)依赖javax.xml.bind.DatatypeConverter做数据转换,这就是迁移后报错的核心原因。

当前解决方案评估

你添加的jaxb-api依赖确实能临时解决问题,但该依赖属于Java EE时代的javax包,与Spring Boot 3的Jakarta生态存在潜在的兼容性冲突风险,并非最优解。

更优解决方案

方案1:升级JJWT至适配Jakarta的版本(推荐)

JJWT从0.11.x版本开始全面适配Jakarta规范,不再依赖javax.xml.bind相关类,同时提供更安全的密钥处理方式。

  1. 更新pom.xml中的JJWT依赖:
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-api</artifactId>
    <version>0.11.5</version>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-impl</artifactId>
    <version>0.11.5</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-jackson</artifactId>
    <version>0.11.5</version>
    <scope>runtime</scope>
</dependency>
  1. 调整代码中的签名方式(新版本推荐使用Keys工具类生成密钥,避免直接传入字符串):
import io.jsonwebtoken.Keys;
import java.nio.charset.StandardCharsets;

// ...

public String generateAccessToken(User user) {
    List<String> roleNames = user.getRoles().stream()
        .map(Role::getRoleType)
        .map(Enum::name)
        .toList();

    return Jwts.builder()
        .setSubject(String.format("%s,%s", user.getId(), user.getEmail()))
        .claim("roles", roleNames)
        .setIssuedAt(new Date())
        .setExpiration(new Date(System.currentTimeMillis() + jwtExpireDate))
        .signWith(Keys.hmacShaKeyFor(jwtSecret.getBytes(StandardCharsets.UTF_8)))
        .compact();
}

方案2:替换为Jakarta版本的JAXB依赖(临时过渡)

如果暂时无法升级JJWT,可使用Jakarta规范的JAXB依赖替代旧的javax版本,适配Spring Boot 3的生态:

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>4.0.0</version>
    <scope>runtime</scope>
</dependency>

总结

优先选择方案1升级JJWT版本,这是符合Spring Boot 3技术栈的长期维护方案;若受限于依赖兼容性暂时无法升级,方案2比你当前的javax依赖更适配Jakarta生态,能降低潜在冲突风险。

内容的提问来源于stack exchange,提问作者Levent Kaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:22:55