gpt4 book ai didi

java - Swagger @ApiModelProperty 示例值为 Long 为 null

转载 作者:行者123 更新时间:2023-12-01 14:17:40 26 4
gpt4 key购买 nike

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

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

我已经试过了
@ApiModelProperty(value = "legacyId", example = null)
public Long getLegacyId() {
return legacyId;
}

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

最佳答案

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

  • 你可以定义为
    @ApiModelProperty(example = "null") --> This will display as "null"

    这会误导用户并可能导致 NPE
  • @ApiModelProperty(hidden = true)

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

    关于java - Swagger @ApiModelProperty 示例值为 Long 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49032676/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com