Swagger @ApiModelProperty example value null for L

2019-08-16 21:01发布

问题:

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
}

I've already tried

@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 ?

回答1:

As you can see here, there is no null dataType. You have two options

  1. You can define as

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

    This will mislead the user and might lead to NPE

  2. @ApiModelProperty(hidden = true)

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.