m1w*_*ell 6 java swagger swagger-ui spring-boot springfox
在我的 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<>();
}
Run Code Online (Sandbox Code Playgroud)
post 请求的 swagger 示例值:
{
"firstName": "John",
"lastName": "Doe",
"phoneNumberDTOList": [
{
"label": "Company",
"number": "put number here"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我认为name = ...属性@ApiModelProperty覆盖了变量名phoneNumberDTOList,但这不起作用:(
我使用 springfox-swagger 2.9.2
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
请检查一下这个问题:
我们不希望出现序列化模型与记录的模型不同的情况。
实际上 的存在
@ApiModelProperty是因为我们想要使用swagger-core使用的相同注释。然而,我们的理念是使用注释只是为了补充文档。例如,如果您使用@JsonProperty等注释了您的模型,我们不想重复使用@ApiModelProperty,因为它很容易不同步。
有一个使用@JsonProperty注释的解决方法:
...
@JsonProperty("phonenumbers")
@ApiModelProperty(value = "list of phone numbers")
List<PhoneNumberDTO> phoneNumberDTOList = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10990 次 |
| 最近记录: |