gpt4 book ai didi

java - 为什么@ApiModelProperty "name"属性没有效果?

转载 作者:行者123 更新时间:2023-11-30 02:00:22 25 4
gpt4 key购买 nike

在我的 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<>();
}

post 请求的 swagger 示例值:

{
"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'


我做错了什么?

最佳答案

请检查此问题:

@ApiModelProperty "name" attribute has no effect

  • We don't want ever to have a case where the serialization model is different from whats being documented.

  • Actually the existence of @ApiModelProperty is explained by the fact that we want to use the same annotations that swagger-core uses. However we take the philosophy of using the annotations just to supplement documentation. If for e.g. you've annotated your models with @JsonProperty etc we don't want to duplicate that using @ApiModelProperty as it is very easy to get out of sync.

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

...

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

关于java - 为什么@ApiModelProperty "name"属性没有效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com