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

Swagger报‘Required field is not provided’错误原因排查请求

Swagger请求报“Required field is not provided”验证错误排查求助

我在使用Swagger调用接口时,出现了“Required field is not provided”的验证错误,相关代码、配置文件如下,请求协助排查错误原因:

控制器代码

public ResponseEntity<Document> fetchRep(
    @Parameter(name = "fromDate", description = "${AbcController.fromDate.ApiParam.value}",
      required = true,
      example = "2023-01-11", schema = @Schema(type = "String"),
      allowEmptyValue = false) @RequestHeader(value = "fromDate", required = true) String fromDate,
    @Parameter(name = "toDate", description = "${AbcController.toDate.ApiParam.value}",
      required = true,
      example = "2023-01-11",
      schema = @Schema(type = "String"), allowEmptyValue = false) @RequestHeader(value = "toDate", required = true) String toDate
)

Swagger报错截图

链接:https://i.sstatic.net/mQes5.png

YAML配置文件

AbcController:
 fromDate:
   ApiParam:
    value: 'Holds the date from which the report data is generated. [yyyy-mm-dd]'
 toDate:
   ApiParam:
    value: 'Holds the date till which the report data is generated [yyyy-mm-dd]'

可能的排查方向

  • 确认Swagger页面中请求头的fromDate和toDate是否已正确填写值,且格式符合yyyy-mm-dd,避免输入为空或格式错误。
  • 检查请求头参数名的大小写一致性:虽然HTTP请求头通常不区分大小写,但部分框架或Swagger版本可能对参数名大小写敏感,确保代码定义的参数名和Swagger页面显示的完全一致。
  • 排查注解配置冲突:@Parameter和@RequestHeader都标记了required=true,部分Swagger版本可能存在注解解析异常,可尝试仅保留@RequestHeader的required=true,移除@Parameter中的required配置后重试。
  • 验证配置文件占位符是否正常解析:确认${AbcController.fromDate.ApiParam.value}这类占位符已被正确加载,若配置文件未生效,可能导致Swagger注解解析出错。
  • 检查Swagger依赖版本:旧版本Swagger(如springfox系列)在处理请求头必填参数时可能存在bug,尝试升级到对应依赖的稳定版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 07:27:44