【问题标题】:Swagger Annotation to add a json example to the response classSwagger Annotation 为响应类添加一个 json 示例
【发布时间】:2017-11-03 09:04:50
【问题描述】:

如何使用 swagger 注释插入默认值而不是“{}”?

【问题讨论】:

  • 显示您当前使用的代码/注释会很有帮助。通常,您在 @ApiOperation / @ApiResponse 响应值中指定一个响应类。示例值通过@ApiModel 和相关注释提供。

标签: java swagger-ui spring-restcontroller


【解决方案1】:

您需要使用 @ApiResponses@ApiResponse 注释您的 Rest 方法。

例如

@ApiResponses(value = { 
                        @ApiResponse(code = 200, message = "Successful retrieval of demand",responseContainer="List"),
                        @ApiResponse(code = 404, message = "Demand does not exist", response = Your Error response.class),
                        @ApiResponse(code = 500, message = "Internal server error", response = Your Error response.class) 
                    }
            )

@ApiModel 注释您的 Json Response 类,并用 @ApiModelProperty 之类的字段注释

@ApiModel(description = "Your comment")
public class Demand implements Serializable {

    private static final long serialVersionUID = 1L;

    
    @ApiModelProperty(notes = "Your comments", required = true, example = "example value")
    private String demandId;
}

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 2020-11-21
    • 1970-01-01
    • 2019-05-18
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多