常用到的注解有:
  • Api
  • ApiModel
  • ApiModelProperty
  • ApiOperation
  • ApiParam
  • ApiResponse
  • ApiResponses
  • ResponseHeader
1. api标记

Api 用在类上,说明该类的作用。可以标记一个Controller类做为swagger 文档资源,使用方式:

  1. @Api(value = "/user", description = "Operations about user")

与Controller注解并列使用。 属性配置:

属性名称 备注
value url的路径值
tags 如果设置这个值、value的值会被覆盖
description 对api资源的描述
basePath 基本路径可以不配置
position 如果配置多个Api 想改变显示的顺序位置
produces For example, "application/json, application/xml"
consumes For example, "application/json, application/xml"
protocols Possible values: http, https, ws, wss.
authorizations 高级特性认证时配置
hidden 配置为true 将在文档中隐藏

在SpringMvc中的配置如下:

  1. @Controller
  2. @RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
  3. @Api(value = "/pet", description = "Operations about pets")
  4. public class PetController {
  5. }
2. ApiOperation标记

ApiOperation:用在方法上,说明方法的作用,每一个url资源的定义,使用方式:

  1. @ApiOperation(
  2. value = "Find purchase order by ID",
  3. notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
  4. response = Order,
  5. tags = {"Pet Store"})

与Controller中的方法并列使用。
属性配置:

属性名称 备注
value url的路径值
tags 如果设置这个值、value的值会被覆盖
description 对api资源的描述
basePath 基本路径可以不配置
position 如果配置多个Api 想改变显示的顺序位置
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".,其他无效
httpMethod "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH"
code http的状态码 默认 200
extensions 扩展属性

在SpringMvc中的配置如下:

  1. @RequestMapping(value = "/order/{orderId}", method = GET)
  2. @ApiOperation(
  3. value = "Find purchase order by ID",
  4. notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
  5. response = Order.class,
  6. tags = { "Pet Store" })
  7. public ResponseEntity<Order> getOrderById(@PathVariable("orderId") String orderId)
  8. throws NotFoundException {
  9. Order order = storeData.get(Long.valueOf(orderId));
  10. if (null != order) {
  11. return ok(order);
  12. } else {
  13. throw new NotFoundException(404, "Order not found");
  14. }
  15. }
3. ApiParam标记

ApiParam请求属性,使用方式:

  1. public ResponseEntity<User> createUser(@RequestBody @ApiParam(value = "Created user object", required = true) User user)

与Controller中的方法并列使用。

属性配置:

属性名称 备注
name 属性名称
value 属性值
defaultValue 默认属性值
allowableValues 可以不配置
required 是否属性必填
access 不过多描述
allowMultiple 默认为false
hidden 隐藏该属性
example 举例子

在SpringMvc中的配置如下:

  1. public ResponseEntity<Order> getOrderById(
  2. @ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true)
  3. @PathVariable("orderId") String orderId)
4. ApiResponse

ApiResponse:响应配置,使用方式:

  1. @ApiResponse(code = 400, message = "Invalid user supplied")

与Controller中的方法并列使用。 属性配置:

属性名称 备注
code http的状态码
message 描述
response 默认响应类 Void
reference 参考ApiOperation中配置
responseHeaders 参考 ResponseHeader 属性配置说明
responseContainer 参考ApiOperation中配置

在SpringMvc中的配置如下:

  1. @RequestMapping(value = "/order", method = POST)
  2. @ApiOperation(value = "Place an order for a pet", response = Order.class)
  3. @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
  4. public ResponseEntity<String> placeOrder(
  5. @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
  6. storeData.add(order);
  7. return ok("");
  8. }
5. ApiResponses

ApiResponses:响应集配置,使用方式:

  1. @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

与Controller中的方法并列使用。 属性配置:

属性名称 备注
value 多个ApiResponse配置

在SpringMvc中的配置如下:

  1. @RequestMapping(value = "/order", method = POST)
  2. @ApiOperation(value = "Place an order for a pet", response = Order.class)
  3. @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
  4. public ResponseEntity<String> placeOrder(
  5. @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
  6. storeData.add(order);
  7. return ok("");
  8. }
6. ResponseHeader

响应头设置,使用方法

  1. @ResponseHeader(name="head1",description="response head conf")

与Controller中的方法并列使用。 属性配置:

属性名称 备注
name 响应头名称
description 头描述
response 默认响应类 Void
responseContainer 参考ApiOperation中配置

在SpringMvc中的配置如下:

  1. @ApiModel(description = "群组")
7. 其他
  • @ApiImplicitParams:用在方法上包含一组参数说明;
  • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    • paramType:参数放在哪个地方
    • name:参数代表的含义
    • value:参数名称
    • dataType: 参数类型,有String/int,无用
    • required : 是否必要
    • defaultValue:参数的默认值
  • @ApiResponses:用于表示一组响应;
  • @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息;
    • code: 响应码(int型),可自定义
    • message:状态码对应的响应信息
  • @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候;
  • @ApiModelProperty:描述一个model的属性。

作者:Xiangdong_She
链接:https://www.jianshu.com/p/12f4394462d5
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

swagger常用注解说明的更多相关文章

  1. Swagger 常用注解

    一.Swagger常用注解 1.与模型相关的注解 两个注解: @ApiModel:用在模型类上,对模型类做注释: @ApiModelProperty:用在属性上,对属性做注释 2.与接口相关的注解 六 ...

  2. swagger 常用注解说明

    本内容引用自:https://blog.csdn.net/u014231523/article/details/76522486 常用注解: - @Api()用于类: 表示标识这个类是swagger的 ...

  3. swagger常用注解

    @Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个参数描述 @ApiModel:用对象来接收参数 @Api ...

  4. swagger2常用注解

    常用注解: @Api()用于类: 表示标识这个类是swagger的资源 @ApiOperation()用于方法: 表示一个http请求的操作 @ApiParam()用于方法,参数,字段说明: 表示对参 ...

  5. Swagger2常用注解解析(轻松构建Swagger)

    Swagger2常用注解解析 一.SpringBoot集成Swagger2 二.常用注解解析 具体使用举例说明: 一.SpringBoot集成Swagger2 引入相关jar包 <!-- swa ...

  6. swagger2常用注解说明

    说明: 1.这里使用的版本:springfox-swagger2(2.4)springfox-swagger-ui (2.4) 2.这里是说明常用注解的含义和基本用法(也就是说已经对swagger进行 ...

  7. swagger2的常用注解,传递参数的注意使用方法

    背景介绍: 刚开始的时候,在controller层使用@RequestParam的时候,发现这个参数是必须要输入值的,但是我们有时候必须查询的时候允许参数为空,使用这个注解就不行了. 在集成了swag ...

  8. Swagger2:常用注解说明

    Swagger2常用注解说明 Spring Boot : Swagger 2使用教程:https://www.cnblogs.com/JealousGirl/p/swagger.html 这里只讲述@ ...

  9. Swagger2常用注解和使用方法

    一   引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...

随机推荐

  1. python---数学表达式的分析树实现

    先走一遍, 前面很多知道点,都串起来了. # coding = utf-8 # 使用列表实现栈的功能 class Stack: def __init__(self): self.items = [] ...

  2. [转] babel-plugin-react-css-modules配置

    自己的react项目用到了css-modules,由于不太想在写className时写style.xxx于是google解决方案,找到了这货->babel-plugin-react-css-mo ...

  3. JSP基础知识➣结构及生命周期(一)

    概述 网络服务器需要一个JSP引擎,也就是一个容器来处理JSP页面.容器负责截获对JSP页面的请求.本教程使用内嵌JSP容器的Apache来支持JSP开发. JSP容器与Web服务器协同合作,为JSP ...

  4. 【loj6029】「雅礼集训 2017 Day1」市场&&【uoj#228】基础数据结构练习题

    题解: 这两道题加上区间取min max应该算线段树几道比较不寻常的题目 其实也是挺好理解的 对于区间/d 显然在log次后就会等于0 而我们注意到如果区间中数都相等那么就可以一起除 也就是说每个区间 ...

  5. bzoj 2832

    题解: 首先有一个比较显然的事情是如果我们确定了买的次数这道题就可以简单的贪心了 但是答案和买的次数是什么关系呢.. 好像是可以三分的 所以应该是单峰的 这里用了模拟退火,而且是没有处理失败情况的模拟 ...

  6. asp.net core 2.0 webapi集成signalr

    asp.net core 2.0 webapi集成signalr   在博客园也很多年了,一直未曾分享过什么东西,也没有写过博客,但自己也是汲取着博客园的知识成长的: 这两天想着不能这么无私,最近.N ...

  7. 正则表达式匹配URL或者网址

    正则表达式 (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])? ...

  8. 一起学Hadoop——文件的上传、分发与打包

    如果我们想把文件上传到Hadoop集群中,使用put命令即可.下面的语句是将本地文件上传到hadoop集群的/目录下. hadoop fs -put fruit.txt /   下面介绍通过脚本将文件 ...

  9. Ant之build.xml详解

    Ant之build.xml详解 关键字: ant build.xml Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译 ...

  10. Django时区的解释

    https://segmentfault.com/q/1010000000405911