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

OpenAPI 3.0生成的/movie端点报415 Unsupported Media Type错误

排查POST /movie端点415 Unsupported Media Type错误的方案

结合你提供的OpenAPI定义、生成的Controller代码和问题描述,以下是具体的排查和解决方向:

1. 枚举类型参数的解析问题(核心原因)

你新增的category和ageCategory是枚举类型,生成的Controller用@RequestPart接收对应的枚举对象,但multipart/form-data请求中,非文件字段默认的Content-Type是text/plain,Spring无法直接将纯字符串格式的枚举值(比如HORROR)反序列化为枚举对象,这会触发415错误。

解决方式:

方案一:改用@RequestParam接收枚举字符串,手动转换

修改Controller方法参数,将枚举类型的@RequestPart替换为@RequestParam接收String,再手动转为枚举:

// 原参数
@ApiParam(value = "", allowableValues = "HORROR") @Valid @RequestPart(value = "category", required = false) CategoryModelApi category
// 修改后
@ApiParam(value = "", allowableValues = "HORROR") @RequestParam(value = "category", required = false) String categoryStr
// 手动转换
CategoryModelApi category = categoryStr != null ? CategoryModelApi.valueOf(categoryStr) : null;

方案二:在Postman中为枚举字段指定Content-Type

在Postman的form-data面板中,找到category和ageCategory字段:

  • 点击字段右侧的Content-Type下拉框,选择application/json
  • 将枚举值用JSON字符串包裹,比如输入"HORROR"(带双引号)

2. 简单字段的接收方式优化

生成的Controller用@RequestPart接收title、description等简单字符串类型,虽然语法上允许,但@RequestPart更适合复杂对象或文件,简单字段建议改用@RequestParam,避免潜在的解析冲突:

// 原参数
@Valid @RequestPart(value = "title", required = false) String title
// 修改后
@RequestParam(value = "title", required = false) String title

3. 检查OpenAPI枚举定义的完整性

确保Category和AgeCategory的Schema定义中明确列出了枚举值,比如:

components:
  schemas:
    Category:
      type: string
      enum: [HORROR, COMEDY, ACTION] # 补充完整枚举值
    AgeCategory:
      type: string
      enum: [PG13, R, PG]

完整的枚举定义能让代码生成工具生成正确的枚举类,避免解析时的类型不匹配。

4. 确认Spring Multipart配置

如果使用的是Spring Boot 2.x及以上版本,默认已经自动配置了StandardServletMultipartResolver,无需额外配置。但可以检查application.properties中是否有冲突的配置:

# 确保没有禁用multipart支持
spring.servlet.multipart.enabled=true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 16:55:20