Swagger注解-@ApiResponses 和 @ApiResponse

时间:2025-03-19 12:16:02

Swagger注解-@Api
Swagger注解-@ApiOperation
Swagger注解-@ApiImplicitParams 和 @ApiImplicitParam
Swagger注解-@ApiModel 和 @ApiModelProperty
Swagger注解-@ApiResponses 和 @ApiResponse
Swagger注解-@ResponseHeader
Swagger注解-@ApiParam
Swagger注解-@Authorization 和 @AuthorizationScope
Swagger注解-@SwaggerDefinition
Swagger注解-@ExternalDocs
Springboot 集成 Swagger GitHub 地址


@ApiResponses

代码示例

github 代码示例: /CoderFreeMan/swagger-demo

使用场景

在 Rest 接口上使用,用作返回值的描述

概述

一个包装器,允许列出多个 ApiResponse,若果你需要描述单个 ApiResponse,你仍然必须使用此注解并将@ApiResponse 包装在一个数组中

属性

属性名称 数据类型 默认值 说明
value ApiResponse[] ApiResponse 列表

@ApiResponse

使用场景

在 Rest 接口或类上和 @ApiResponses 组合使用,组装返回参数说明

概述

描述操作的可能响应,这可用于描述 Rest API 调用中可能的成功和错误 code 码。此注解可以在方法或类级别应用;仅当在方法级别或抛出时未定义相同代码的@ApiResponse注解时才会解析类级别注解异常,如果响应中使用了不同的响应类,则可以通过将响应类于响应码组合使用。注意swagger不允许单个响应代码的多个响应类型。此注解不直接使用,单独使用时swagger不会解析,应该和ApiResponses组合使用。

属性

属性名称 数据类型 默认值 说明
code int 响应的HTTP状态码
message String 伴随响应的人类可读的消息
response Class<?> 用于描述消息有效负载的可选响应类,对应于响应消息对象的 schema 字段
reference String “” 指定对响应类型的引用,指定的应用可以使本地引用,也可以是远程引用,将按原样使用,并将覆盖任何指定的response()类
responseHeaders ResponseHeader[] @ResponseHeader(name = “”, response = ) 可能响应的 header 列表
responseContainer String “” 声明响应的容器,有效值为List,Set,Map,任何其他值都将被忽略

Swagger注解-@Api
Swagger注解-@ApiOperation
Swagger注解-@ApiImplicitParams 和 @ApiImplicitParam
Swagger注解-@ApiModel 和 @ApiModelProperty
Swagger注解-@ApiResponses 和 @ApiResponse
Swagger注解-@ResponseHeader
Swagger注解-@ApiParam
Swagger注解-@Authorization 和 @AuthorizationScope
Swagger注解-@SwaggerDefinition
Swagger注解-@ExternalDocs
Springboot 集成 Swagger GitHub 地址