swagger注解 详细说明

swagger注解 详细说明swagger

常用到的注解有:

1. api标记

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

@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中的配置如下:

@Controller @RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}) @Api(value = "/pet", description = "Operations about pets") public class PetController { }

2. ApiOperation标记

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

@ApiOperation( value = "Find purchase order by ID", notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions", response = Order, 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中的配置如下:

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

3. ApiParam标记

ApiParam请求属性,使用方式:

public ResponseEntity 
   
   createUser(@RequestBody @ 
   ApiParam( 
   value = 
   "Created user object", required = 
   true) User user) 
  
属性名称 备注 name 属性名称 value 属性值 defaultValue 默认属性值 allowableValues 可以不配置 required 是否属性必填 access 不过多描述 allowMultiple 默认为false hidden 隐藏该属性 example 举例子

在SpringMvc中的配置如下:

 public ResponseEntity 
   
   getOrderById( @ 
   ApiParam( 
   value = 
   "ID of pet that needs to be fetched", allowableValues = 
   "range[1,5]", required = 
   true) @PathVariable( 
   "orderId") String orderId) 
  

4. ApiResponse

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 
   
   placeOrder( @ 
   ApiParam(value = 
   "order placed for purchasing the pet", required = 
   true) Order order) { storeData.add(order); 
   return ok( 
   ""); } 
  

5. ApiResponses

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 
   
   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中配置

在SpringMvc中的配置如下:

@ApiModel(description = "群组")

7. 其他


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221117.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午6:53
下一篇 2026年3月17日 下午6:53


相关推荐

  • T25 截图识别文字工具

    T25 截图识别文字工具截图识别文字工具

    2022年5月1日
    44
  • Python将数据写入txt文件_python将内容写入txt文件

    Python将数据写入txt文件_python将内容写入txt文件一、读写txt文件1、打开txt文件Note=open(‘x.txt’,mode=’w’)函数=open(x.扩展名,mode=模式)模式种类:w只能操作写入(如果而文件中有数据,再次写入内容,会把原来的覆盖掉)r只能读取a向文件追加w+可读可写r+可读可写a+可读可追加wb+写入进制数据2、向文件中写入数据第一种写入方式:write写入Note.write…

    2022年10月3日
    4
  • 好的图片压缩网站

    好的图片压缩网站转:第一:Tinypng地址:https://tinypng.com/这款工具我实在是太喜欢了,经常用到,最大限度的做到对画质无损的进行压缩这个工具他同时支持对Jpg和Png的压缩。Tinypng也支持Wordpress和magento的使用。Wordpress插件下载:https://wordpress.org/plugins/tiny-compress-images/…

    2022年6月18日
    39
  • power命令_kernel power41

    power命令_kernel power411/*kernel/power/earlysuspend.c2*3*Copyright(C)2005-2008Google,Inc.4*5*ThissoftwareislicensedunderthetermsoftheGNUGeneralPublic6*Licenseve…

    2026年1月17日
    8
  • qpainter画箭头改变方向_visio如何画箭头

    qpainter画箭头改变方向_visio如何画箭头画箭头需要注意:计算箭头两个线的位置和长度与直线或弧线间的位置关系。1.画直线箭头关键代码constintlength=10;//箭头斜着的投影到线上的长度QVector<QLineF>lines;lines.append(QLineF(20,height()/2,width()/2,height()/2));li…

    2025年7月6日
    5
  • gateway网关详解_网关怎么设置才能上网

    gateway网关详解_网关怎么设置才能上网本文介绍了微服务中Gateway的使用,正在学习Gateway或者准备学习的大佬看过来哟

    2022年10月11日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号