【问题标题】:Swagger @ApiModelProperty example value null for LongSwagger @ApiModelProperty 示例值 null 为 Long
【发布时间】:2018-08-08 12:24:15
【问题描述】:

我使用 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】:

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

    1. 你可以定义为

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

      这样会误导用户,可能导致NPE

    2. @ApiModelProperty(hidden = true)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多