Long 的 Swagger @ApiModelProperty 示例值为 null

Swagger @ApiModelProperty example value null for Long

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

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

我已经试过了

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

但是我收到警告 "Attribute value must be constant"。我还能做什么?

如您所见here,没有空数据类型。你有两个选择

  1. 你可以定义为

    @ApiModelProperty(example = "null") --> This will display as "null"
    

    这会误导用户并可能导致 NPE

  2. @ApiModelProperty(hidden = true)

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