Swagger / Springfox自动生成响应示例

Tli*_*ink 7 api rest swagger spring-boot springfox

当前使用springfox 2.9.2来Swagger文档在Spring中创建的API。我想在文档中添加示例响应,如下图所示;

在此处输入图片说明

我的理解是,我可以做类似的事情:

@ApiResponses(value = {
    @ApiResponse(code = 200, message = "Success", 
            examples = @io.swagger.annotations.Example(
                value = {
                    @ExampleProperty(value = "{'snapshot'?{'type': 'AAA'}}", mediaType = "application/json") 
                }))
Run Code Online (Sandbox Code Playgroud)

GET在这种情况下,我将此代码段放在方法上方。不幸的是,上面的2个示例始终显示:标识符预期错误

但我也看到我也可以这样做:

@ApiResponses(value = {
    ApiResponse(code = 200, message = "Success", response = MyModel.class,
    )
})
Run Code Online (Sandbox Code Playgroud)

另外我看到我可以添加一个带有@ApiOperation级别的示例:

@ApiOperation(value = "Create a Account", nickname = "createAccount", notes = "Create a account", response = AccountResponse.class, tags={  })
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 如何在Swagger文档中添加示例JSON响应?

  2. 将Swagger / Springfox指向我的模型/ bean并使其自动生成示例响应,并随着bean /模型的每次更新自动更新,将是理想的选择。这是上面第二个代码段应该做的吗?

ole*_*nik 3

使用 dto 的注释定义示例:

@ApiModel("Crop")
public class CropDto {

    @ApiModelProperty(name = "Unique guid", position = 1, example = "7aaee0e2-6884-4fd7-ba63-21d76723dce2")
    public UUID id;
    @ApiModelProperty(name = "Unique code", position = 2, example = "squ")
    public String code;
    @ApiModelProperty(name = "Unique name", position = 3, example = "Squash")
    public String name;
    @ApiModelProperty(position = 4, example = "Cucurbita pepo L.")
    public String description;
}
Run Code Online (Sandbox Code Playgroud)