Swagger2.X注解
常用到的注解有:
| 作用范围 | API | 使用位置 |
|---|---|---|
| 协议集描述 | @Api | 用于controller类上 |
| 协议描述 | @ApiOperation | 用在controller的方法上 |
| 非对象参数集 | @ApiImplicitParams | 用在controller的方法上 |
| 非对象参数描述 | @ApiImplicitParam | 用在@ApiImplicitParams的方法里边 |
| 对象参数描述 | @ApiParam | 用在@ApiImplicitParams的方法里边,定义接收的参数形式 |
| 描述返回对象的意义 | @ApiModel | 用在返回对象类上 |
| 对象属性 | @ApiModelProperty | 用在参数对象的字段上 |
| Response集 | @ApiResponses | 用在controller的方法上 |
| Response | @ApiResponse | 用在 @ApiResponses里边 |
| Response | @ResponseHeader |
1. @Api标记
Api 用在类上,说明该类的作用。可以标记一个 Controller 类做为 swagger 文档资源,使用方式:
与Controller注解并列使用。 属性配置
| 属性名称 | 备注 | 是否弃用 |
|---|---|---|
| value | url的路径值 | |
| tags | 如果设置这个值、value的值会被覆盖 | |
| description | 对api资源的描述 | @Deprecated |
| basePath | 基本路径可以不配置 | @Deprecated |
| position | 如果配置多个Api 想改变显示的顺序位置 | @Deprecated |
| produces | For example, "application/json, application/xml" | |
| consumes | For example, "application/json, application/xml" | |
| protocols | Possible values: http, https, ws, wss. | |
| authorizations | 高级特性认证时配置 |
在SpringMvc中的配置如下:
@RestController
@RequestMapping(value = "/app/table")
@Api(value = "/app/table", protocols = "http")
public class AppTableController {
}
2. @ApiOperation标记
ApiOperation:用在方法上,说明方法的作用,每一个url资源的定义,使用方式:
@ApiOperation(
value = "Find purchase order by ID",
notes = "",
response = A.class,
tags = {""})
与Controller中的方法并列使用。
属性配置:
| 属性名称 | 备注 | 是否弃用 |
|---|---|---|
| value | url的路径值 | |
| tags | 如果设置这个值、value的值会被覆盖 | |
| description | 对api资源的描述 | 无 |
| basePath | 基本路径可以不配置 | 无 |
| position | 如果配置多个Api 想改变显示的顺序位置 | @Deprecated |
| produces | For example, "application/json, application/xml" | |
| consumes | For example, "application/json, application/xml" | |
| protocols | Possible values: http, https, ws, wss. | |
| authorizations | 高级特性认证时配置 | |
| hidden | 配置为true 将在文档中隐藏 | |
| response | 返回的对象 | |
| responseContainer | 这些对象是有效的 "List", "Set" or "Map".,其他无效 | |
| responseReference | 指定对响应类型的引用。指定的引用可以是本地的,也可以是远程的*将按原样使用,并覆盖任何指定的response()类。 | |
| responseHeaders | 响应旁边提供的可能标题列表。 | |
| httpMethod | "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH" | |
| code | http的状态码 默认 200 | |
| extensions | 扩展属性 | |
| notes | 注释说明 | |
| response | 方法的返回值的类型class |
在SpringMvc中的配置如下:
@ApiOperation(value = "/createCode", notes = "创建文件", response = ResultObj.class)
@RequestMapping(value = "/createCode")
public ResultObj createCode(
@RequestParam(value = "tab_name", required = true) String tab_name
, @RequestParam(value = "type", required = true) Integer type
, @RequestParam(value = "includeSwagger", required = true) Boolean includeSwagger
, @RequestParam(value = "moduleName", required = true) String moduleName
, @RequestParam(value = "packageName", required = true) String packageName) {
return ResultObj.getDefaultResponse(appTableService.listTable(tab_name, null));
}
3. @ApiImplicitParams、@ApiImplicitParam、@ApiParam标记
@ApiImplicitParams
只有value一个属性,用来装多个@ApiImplicitParam。
@ApiImplicitParam
用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
| 属性名称 | 备注 |
|---|---|
| paramType | 参数放在哪个地方 |
| name | 参数名称 |
| value | 参数代表的含义 |
| dataType | 参数类型 |
| dataTypeClass | 参数类型 |
| required | 是否必要 |
| defaultValue | 参数的默认值 |
paramType:
header-->请求参数的获取:@RequestHeader(代码中接收注解)
query-->请求参数的获取:@RequestParam(代码中接收注解)
path(用于restful接口)-->请求参数的获取:@PathVariable(代码中接收注解)
body-->请求参数的获取:@RequestBody(代码中接收注解)
form(不常用))
使用方式
@ApiOperation(value = "/createCode", notes = "创建文件", response = ResultObj.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "表名", value = "tab_name", paramType = "query", dataType = "String", required = true)
, @ApiImplicitParam(name = "生成代码类别参数", value = "type", paramType = "query", dataType = "Integer", required = true)
})
@RequestMapping(value = "/createCode")
public ResultObj createCode(
@RequestParam(value = "tab_name", required = true) String tabName
, @RequestParam(value = "type", required = true, defaultValue = "-1") Integer type){return null;}
@ApiParam
用在接收参数中,指定一个请求参数的信息
| 属性名称 | 备注 |
|---|---|
| name | 要绑定到的请求参数的名称 |
| value | 参数的简单描述 |
| required | 是否必要 |
| defaultValue | 参数的默认值 |
具体参考文档v1.5.0
@ApiParam请求属性,使用方式:与Controller中的方法并列使用。
属性配置:
| 属性名称 | 备注 |
|---|---|
| name | 属性名称 |
| value | 属性值 |
| defaultValue | 默认属性值 |
| allowableValues | 可以不配置 |
| required | 是否属性必填 |
| access | 不过多描述 |
| allowMultiple | 默认为false |
| hidden | 隐藏该属性 |
| example | 举例子 |
在SpringMvc中的配置如下:
public ResponseEntity<Order> getOrderById(
@ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true)
@PathVariable("orderId") String orderId)
4. @ApiModel、@ApiModelProperty
@ApiModel
描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用 @ApiImplicitParam注解进行描述的时候;
| 属性名称 | 备注 |
|---|---|
| value | 名称 |
| description | 描述 |
| parent | 父类 (class) |
| discriminator | 官方描述:支持模型继承和多态性。这是用作鉴别器的字段名。基于这个领域,可以断言需要使用哪种子类型。 |
| subTypes | 子类型 {(class)} |
| reference | 官方描述:指定对相应类型定义的引用,覆盖指定的任何其他元数据 |
@ApiModelProperty
描述一个model的属性。
@Table(name = "app_table", schema = "", catalog = "")
@ApiModel(value = "物理表信息表", description = "当前数据库表信息")
public class AppTable implements Serializable {
private static final long serialVersionUID = -1L;
@Id
@Column(name = "tab_name", unique = true)
@ApiModelProperty(value = "表名主键")
5. @ApiResponses、@ApiResponse
@ApiResponses:响应集配置,使用方式:
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
与Controller中的方法并列使用。 属性配置:
| 属性名称 | 备注 |
|---|---|
| value | 多个ApiResponse配置 |
在SpringMvc中的配置如下:
@RequestMapping(value = "/order", method = POST)
@ApiOperation(value = "Place an order for a pet", response = Order.class)
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
public ResponseEntity<String> placeOrder(
@ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
storeData.add(order);
return ok("");
}
@ApiResponse:响应配置,使用方式:
@ApiResponse(code = 400, message = "Invalid user supplied")
与Controller中的方法并列使用。 属性配置:
| 属性名称 | 备注 |
|---|---|
| code | http的状态码 |
| message | 描述 |
| response | 默认响应类 Void |
| reference | 参考ApiOperation中配置 |
| responseHeaders | 参考 ResponseHeader 属性配置说明 |
| responseContainer | 参考ApiOperation中配置 |
在SpringMvc中的配置如下:
@RequestMapping(value = "/order", method = POST)
@ApiOperation(value = "Place an order for a pet", response = Order.class)
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
public ResponseEntity<String> placeOrder(
@ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
storeData.add(order);
return ok("");
}
6. ResponseHeader
响应头设置,使用方法
@ResponseHeader(name="head1",description="response head conf")
与Controller中的方法并列使用。 属性配置:
| 属性名称 | 备注 |
|---|---|
| name | 响应头名称 |
| description | 头描述 |
| response | 默认响应类 Void |
| responseContainer | 参考ApiOperation中配置 |
Swagger2.X注解的更多相关文章
- swagger2常用注解
常用注解: @Api()用于类: 表示标识这个类是swagger的资源 @ApiOperation()用于方法: 表示一个http请求的操作 @ApiParam()用于方法,参数,字段说明: 表示对参 ...
- Swagger2常用注解和使用方法
一 引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...
- Swagger2常用注解解析(轻松构建Swagger)
Swagger2常用注解解析 一.SpringBoot集成Swagger2 二.常用注解解析 具体使用举例说明: 一.SpringBoot集成Swagger2 引入相关jar包 <!-- swa ...
- swagger2 常用注解的使用
一.@Api 效果: @Api注解放在类上面,这里的value是没用的,tags表示该controller的介绍. 二 .@ApiOperation 效果: @ApiOperation注解用于放在方法 ...
- Swagger2基本注解使用
@Api:用在请求的类上,表示对类的说明 tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI界面上也看到,所以不需要配置&q ...
- Swagger2常用注解及其说明 (转)
Api 用在Controller中,标记一个Controller作为swagger的文档资源 属性名称 说明 value Controller的注解 description 对api资源的描述 hid ...
- swagger2常用注解说明
说明: 1.这里使用的版本:springfox-swagger2(2.4)springfox-swagger-ui (2.4) 2.这里是说明常用注解的含义和基本用法(也就是说已经对swagger进行 ...
- swagger2 常用注解说明
常用到的注解有: Api ApiModel ApiModelProperty ApiOperation ApiParam ApiResponse ApiResponses ResponseHeader ...
- Swagger2 @ApiIgnore注解忽略接口在swagger-ui.html中显示
果项目中定义了一个controller,但是我们又不想把这个接口在swagger-ui.html中体现出来怎么办?不要着急,Swagger2已经替我们想到了这个问题,只要把@ApiIgnore放到你想 ...
随机推荐
- 关于torch.nn.LSTM()的输入和输出
主角torch.nn.LSTM() 初始化时要传入的参数 | Args: | input_size: The number of expected features in the input `x` ...
- 【011】JavaSE面试题(十一):多线程(1)
第一期:Java面试 - 100题,梳理各大网站优秀面试题.大家可以跟着我一起来刷刷Java理论知识 [011] - JavaSE面试题(十一):多线程(1) 第1问:线程和进程的区别? 进程:具有一 ...
- dart pub上传失败如何解决
问题: Flutter Exception: Pub will wait for a while before trying to connect again. 解决 1.设置终端代理 export ...
- VSCode 如何远程连接其他主机的 WSL2
VSCode 如何远程连接其他主机的 WSL2 VSCode 的 Remote Deployment 插件对 WSL2 直接提供了支持,能够很方便的连接本机的 WSL2 ,但是并没有提供一个连接远程 ...
- Kafka之--自动启动zookeeper & kafka 脚本
1) 首先配置SSH免密登录,在这里我用kafka(151)这台机器来作为启动脚本的存放和执行机器 [root@kafaka3 .ssh]# pwd #生成SSH KEY /root/.ssh [ro ...
- Qt开源作品38-无边框窗体方案(无抖动,支持win、linux、mac等系统,侧边半屏顶部全屏)
一 前言 不知道各位程序员有没有遇到过这样一种困惑,好不容易在开源网站找到了类似的想要的项目代码,结果down下来一编译,我勒个去,几百个错误,根本没法用,熟悉的人还好可以直接阅读代码进行修改(有些只 ...
- 【NOIP2007】Hanoi双塔问题
题目描述 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆盘是不加区分的(下图为n=3的情形). 现要将这些圆盘移到C柱上 ...
- Spring 框架中都用到了哪些设计模式
Spring 框架中都用到了哪些设计模式? Spring 框架中使用到了大量的设计模式,下面列举了比较有代表性的: 1.代理模式-在 AOP 和 remoting 中被用的比较多. 2.单例模式:在 ...
- 第七篇 -- 常用界面组件的使用(QSlider和QProgressBar)
首先画个图 ui_proBar.py # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui_ ...
- 理清 UT、UTC、GMT和CST
平时各种时间标准简写绕得不明不白,查一下整理在这里. IAT: International Atomic Time, 原子时.又称国际原子时,是一种通过原子钟得到的时间标准,原子钟是世界上已知最准确的 ...