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

如何在Spring Doc中为List类型指定@Schema的type属性?

如何通过@Schema注解的type属性指定List类型的数据类型?

在Spring Boot 3搭配Spring Doc的场景下,要为List<DuplicatePersonName>类型的字段通过@Schema注解的type属性明确指定数据类型,需要结合type和arraySchema属性配置:

@Schema(
    description = "List of duplicate person",
    type = SchemaType.ARRAY,
    arraySchema = @Schema(implementation = DuplicatePersonName.class)
)
List<DuplicatePersonName> duplicatePersons;

说明:

  • type = SchemaType.ARRAY:将字段类型明确标记为OpenAPI规范中的数组类型(集合类型在OpenAPI标准里统一用array表示)。
  • arraySchema = @Schema(implementation = DuplicatePersonName.class):指定数组内每个元素的具体类型为DuplicatePersonName。

如果偏好使用字符串形式的类型值,也可以写成:

@Schema(
    description = "List of duplicate person",
    type = "array",
    arraySchema = @Schema(implementation = DuplicatePersonName.class)
)
List<DuplicatePersonName> duplicatePersons;

通常Spring Doc能自动推断泛型集合的类型,但若出现自动识别不准确的情况,就可以用上述方式手动指定。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 07:37:07