Swagger @ApiModelProperty 示例值为 Long 为 null

naX*_*aXa 4 java annotations swagger springfox

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

{
  /* ... */
  "legacyId": null
}
Run Code Online (Sandbox Code Playgroud)

我已经试过了

@ApiModelProperty(value = "legacyId", example = null)
public Long getLegacyId() {
    return legacyId;
}
Run Code Online (Sandbox Code Playgroud)

但是我收到警告“属性值必须是常量”。我还能做什么?

pvp*_*ran 5

正如您在此处看到的,没有空数据类型。你有两个选择

  1. 你可以定义为

    @ApiModelProperty(example = "null") --> This will display as "null"
    
    Run Code Online (Sandbox Code Playgroud)

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

  2. @ApiModelProperty(hidden = true)

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