如何在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
相关产品推荐
相关产品推荐

