How can I set a description and an example in Swagger with Swagger annotations?(如何使用 Swagger 注释在 Swagger 中设置描述和示例?)
问题描述
我正在使用 Spring boot 创建一个 REST Api,并使用 swagger codegen 在控制器中自动生成 swagger 文档.但是,我无法在 POST 请求中为 String 类型的参数设置描述和示例.这是mi代码:
I am creating a REST Api using Spring boot, and auto generating the swagger documentation in controllers using swagger codegen. However, I am not able to set a description and example for a parameter of type String in a POST request. Here is mi code:
@ApiParam 的 example 属性是我手动插入的,因为 codegen 忽略了 yaml 的那部分(这是另一个问题:为什么编辑器忽略了 example 部分?). 这是yaml的一部分:
The example property of the @ApiParam has been manually inserted by me, because the codegen was ignoring that part of the yaml (That is another question: why is the editor ignoring the example part?). Here is part of the yaml:
最后,这就是 swagger 所展示的:
And finally, this is what swagger is showing:
最后,build.gradle中用到的依赖如下:
Finally, the dependencies used in build.gradle are the following ones:
所以,问题是:有谁知道我如何使用 swagger 注释来设置 body 参数的描述和示例?
So, Question is: Does anybody know how I can set the description and an example of a body parameter using swagger annotations?
编辑
我已经使用@ApiImplicitParam 而不是@ApiParam 来更改描述,但是仍然缺少示例:
I've achieved to change the description using @ApiImplicitParam instead of @ApiParam, but example is still missing:
推荐答案
我在为 body 对象生成示例时遇到了类似的问题 - 注释 @Example
和 @ExampleProperty
根本没有在 swagger 1.5.x 中不能无缘无故地工作.(我用的是 1.5.16)
I have similar issue with generating examples for body objects - annotation @Example
and @ExampleProperty
simply doesn't work for no reason in swagger 1.5.x. (I use 1.5.16)
我目前的解决方案是:
将 @ApiParam(example="...")
用于非实体对象,例如:
My current solution is:
use @ApiParam(example="...")
for non-body objects, e.g.:
为 body 对象创建新类并使用 @ApiModelProperty(value = " ", example = " ")
注释字段,例如:
for body objects create new class and annotate fields with @ApiModelProperty(value = " ", example = " ")
, e.g.:
这篇关于如何使用 Swagger 注释在 Swagger 中设置描述和示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!