【问题标题】:Why does @ApiModelProperty "name" attribute has no effect?为什么@ApiModelProperty "name" 属性不起作用?
【发布时间】:2019-04-04 14:16:11
【问题描述】:

在我的 Spring Boot 应用程序中,我有一个 DTO 对象,其中包含 DTO 对象的嵌套列表。 类:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "contact")
public class ContactDTO {
  @ApiModelProperty(value = "id", example = "1", hidden = true)
  private Long id;

  @ApiModelProperty(value = "first name", example = "John")
  private String firstName;

  @ApiModelProperty(value = "last name", example = "Doe")
  private String lastName;

  @Builder.Default
  @ApiModelProperty(value = "list of phone numbers", name = "phonenumbers")
  List<PhoneNumberDTO> phoneNumberDTOList = new ArrayList<>();
}

发布请求的招摇示例值:

{
  "firstName": "John",
  "lastName": "Doe",
  "phoneNumberDTOList": [
    {
      "label": "Company",
      "number": "put number here"
    }
  ]
}


我以为@ApiModelProperty 中的name = ... 属性会覆盖变量名phoneNumberDTOList,但这不起作用:(

我使用springfox-swagger 2.9.2

  implementation 'io.springfox:springfox-swagger2:2.9.2'
  implementation 'io.springfox:springfox-swagger-ui:2.9.2'


我做错了什么?

【问题讨论】:

    标签: java spring-boot swagger swagger-ui springfox


    【解决方案1】:

    请查看此问题:

    @ApiModelProperty "name" attribute has no effect

    • 我们不希望出现序列化模型与文档不同的情况。

    • 实际上@ApiModelProperty 的存在是因为我们想使用与swagger-core 相同的注解。然而,我们采用使用注释来补充文档的理念。如果例如你已经用@JsonProperty 等注释了你的模型,我们不想使用@ApiModelProperty 复制它,因为它很容易不同步。

    @JsonProperty 注释有一个解决方法:

    ...
    
    @JsonProperty("phonenumbers")
    @ApiModelProperty(value = "list of phone numbers")
    List<PhoneNumberDTO> phoneNumberDTOList = new ArrayList<>();
    

    【讨论】:

    • 哇,这个问题是从 2016 年 11 月开始的...感谢您 - 与 @JsonProperty 一起使用
    • @JsonProperty 会在 SwaggerUI 上消失列名,还有其他选择吗?
    猜你喜欢
    • 2019-02-19
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多