Swagger @ApiModelProperty 示例值为 Long 为 null [英] Swagger @ApiModelProperty example value null for Long
问题描述
我将 SpringFox 和 Swagger UI 用于 API 文档.
我有一个 DTO,其中有一个 Long 类型的属性.它没有 99% 的时间被填充,所以我想通过将属性值设置为 null
在文档中演示这个事实.所以我想在示例部分使用这个 JSON
I use SpringFox and Swagger UI for API documentation.
I have a DTO in which there is a property which is of type Long. It's not populated 99% of time so I want to demonstrate this fact in documentation by setting the property value to null
. So I want this JSON in examples section
{
/* ... */
"legacyId": null
}
我已经试过了
@ApiModelProperty(value = "legacyId", example = null)
public Long getLegacyId() {
return legacyId;
}
但我收到警告属性值必须是常量".我还能做什么?
But I got a warning "Attribute value must be constant". What else I can do ?
推荐答案
如你所见 这里,没有空数据类型.你有两个选择
As you can see here, there is no null dataType. You have two options
你可以定义为
You can define as
@ApiModelProperty(example = "null") --> This will display as "null"
这会误导用户并可能导致 NPE
This will mislead the user and might lead to NPE
@ApiModelProperty(hidden = true)
就我个人而言,我更喜欢第二个,因为当 spring 从控制器中的 UI 映射 json 时,如果没有从前端传递任何内容,它将自动为 null.
personally, I would prefer second one because, when spring maps the json from UI in your controller, if nothing is passed from frontend, it will be null automatically.
这篇关于Swagger @ApiModelProperty 示例值为 Long 为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!