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

标签 java annotations swagger springfox

我用 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/

    相关文章:

    java - 在 GridBagConstraints 容器上设置背景图像

    java - @WebParam 的@XmlElement(required=true) 不起作用

    java - 你可以专门化继承的Java注释吗

    django - 注释在 Django/Tastypie 中被忽略

    asp.net-web-api - 为什么当我发布到服务器时,NSwag/Swagger 方案仅设置为 HTTP,但在我的本地计算机上却设置为 HTTPS?

    json - 没有固定属性列表的对象的 Swagger Yaml 模式定义

    java - 比较两个整数 : why is == true?

    java - Intellij 无法检测到@Slf4j 使用 Lombok 添加的记录器

    java - 如何从共享非常相似逻辑的子类中提取父类?

    java - 即使将 Swagger 添加为 Maven 依赖项,也无法导入 @ApiParam 或 @ApiModelProperty 注释