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

Spring Boot 3.0.4 WebFlux升级后出现415不支持媒体类型问题

Spring Boot WebFlux 2.7.5升级到3.0.4后415 UNSUPPORTED_MEDIA_TYPE问题解决

升级Spring Boot WebFlux从2.7.5到3.0.4后,原支持XML接收和返回的接口出现「415 UNSUPPORTED_MEDIA_TYPE」错误,接口代码及原依赖如下:

原接口代码

@PostMapping(
    value = "/sides/Request",
    consumes = MediaType.APPLICATION_XML_VALUE,
    produces = MediaType.APPLICATION_XML_VALUE)
public Mono<ResponseModel> getSsi(@RequestBody Mono<RequestModel> requestModelMono) {
    // 业务逻辑
}

原POM依赖

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <scope>runtime</scope>
</dependency>

问题原因

Spring Boot 3.0 基于Jakarta EE规范,彻底取代了旧的Java EE(javax.*包),原javax.xml.bind相关依赖已不再兼容,导致无法处理XML媒体类型。

解决方案

  1. 替换JAXB依赖为Jakarta版本
    移除所有javax.xml.bind相关依赖,替换为Jakarta EE对应的JAXB依赖:

    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>4.0.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>4.0.2</version>
        <scope>runtime</scope>
    </dependency>
    

    注:Spring Boot 3.x会自动管理部分依赖版本,jaxb-impl和jaxb-core建议使用4.x版本以兼容Jakarta API。

  2. 更新实体类注解包路径
    将实体类中所有javax.xml.bind.annotation.*注解替换为jakarta.xml.bind.annotation.*,例如:

    • 原@javax.xml.bind.annotation.XmlRootElement → 替换为@jakarta.xml.bind.annotation.XmlRootElement
    • 原@javax.xml.bind.annotation.XmlElement → 替换为@jakarta.xml.bind.annotation.XmlElement
  3. 检查请求头配置
    确认请求方发送的Content-Type请求头严格为application/xml,避免拼写错误或使用其他类似类型(如text/xml)。

额外排查点

若上述步骤后仍有问题,检查是否存在自定义的HttpMessageConverter配置,确保未覆盖或禁用Spring Boot默认的XML消息转换器。

内容的提问来源于stack exchange,提问作者Chandresh Mishra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 11:02:31