Swagger @ApiModelProperty示例值为null为Long

问题描述 投票:0回答:1

我使用SpringFox和Swagger UI来获取API文档。 我有一个DTO,其中有一个Long类型的属性。它没有99%的时间填充,所以我想在文档中通过将属性值设置为null来证明这一事实。所以我想在示例部分中使用这个JSON

{
  /* ... */
  "legacyId": null
}

我已经试过了

@ApiModelProperty(value = "legacyId", example = null)
public Long getLegacyId() {
    return legacyId;
}

但我收到警告“属性值必须保持不变”。我还能做什么?

java annotations swagger springfox
1个回答
2
投票

正如您所见,here,没有null dataType。你有两个选择

  1. 你可以定义为 @ApiModelProperty(example = "null") --> This will display as "null" 这会误导用户并可能导致NPE
  2. @ApiModelProperty(hidden = true)

个人而言,我更喜欢第二个,因为当spring在控制器中从UI映射json时,如果没有从前端传递任何内容,它将自动为null。

© www.soinside.com 2019 - 2024. All rights reserved.