Swagger2常用注解解析(轻松构建Swagger)
Swagger2常用注解解析
一、SpringBoot集成Swagger2
- 引入相关jar包
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<!-- 官方UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<!-- bootstrap-ui -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
- 编写Swagger2文档
package com.zgx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//使类被spring扫描到
@Configuration
//开启Swagger2
@EnableSwagger2
public class Swagger2 {
//http://localhost:8088/swagger-ui.html //简洁
//http://localhost:8088/doc.html //页面优化
//配置Swagger2核心配置 docket
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2) //指定api为Swagger2
.apiInfo(apiInfo()) //用于定义api汇总信息
.select().apis(RequestHandlerSelectors.
basePackage("com.zgx.controller"))//指定controller包
.paths(PathSelectors.any()) //所有controller
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("基于可穿戴设备的医疗监护系统接口api") //文档页标题
.contact(new Contact("莫逸风",
"https://blog.csdn.net/qq_38723677",
"150****5317@163.com")) //联系人信息
.description("专为可穿戴设备的医疗监护系统提供的api文档") //详细信息
.version("0.0.1") //文档版本号
.termsOfServiceUrl("") //网站地址
.build();
}
}
- 优化Swagger2显示
@ApiIgnore忽略该接口,或类
//类注释
@Api(value = "注册登录",tags = {"用于注册登录的相关接口"})
//方法注释
@ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
//BO实体类注释
@ApiModel(value = "用户对象BO", description = "从客户端,由用户传入的数据封装在此entity中")
//BO实体类变量注释
@ApiModelProperty(value = "用户名", name = "username", example = "moyifeng", required = true)
二、常用注解解析
@Api()用于类;
表示标识这个类是swagger的资源@ApiOperation()用于方法;
表示一个http请求的操作@ApiParam()用于方法,参数,字段说明;
表示对参数的添加元数据(说明或是否必填等)@ApiModel()用于类
表示对类进行说明,用于参数用实体类接收@ApiModelProperty()用于方法,字段
表示对model属性的说明或者数据操作更改@ApiIgnore()用于类,方法,方法参数
表示这个方法或者类被忽略@ApiImplicitParam() 用于方法
表示单独的请求参数@ApiImplicitParams() 用于方法;
包含多个 @ApiImplicitParam
具体使用举例说明:
@Api()
用于类;表示标识这个类是swagger的资源
tags–表示说明
value–也是说明,可以使用tags替代
但是tags如果有多个值,会生成多个list
@Api(value="用户controller",tags={"用户操作接口"})
@RestController
public class UserController {
}
@ApiOperation() 用于方法;表示一个http请求的操作
value用于方法描述
notes用于提示内容
tags可以重新分组(视情况而用)
@ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
name–参数名
value–参数说明
required–是否必填
@Api(value="用户controller",tags={"用户操作接口"})
@RestController
public class UserController {
@ApiOperation(value="获取用户信息",tags={"获取用户信息copy"},notes="注意问题点")
@GetMapping("/getUserInfo")
public User getUserInfo(@ApiParam(name="id",value="用户id",required=true) Long id,@ApiParam(name="username",value="用户名") String username) {
// userService可忽略,是业务逻辑
User user = userService.getUserInfo();
return user;
}
}
@ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收
value–表示对象名
description–描述
都可省略
@ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
example–举例说明
hidden–隐藏
@ApiModel(value="user对象",description="用户对象user")
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@ApiModelProperty(value="用户名",name="username",example="xingguo")
private String username;
@ApiModelProperty(value="状态",name="state",required=true)
private Integer state;
private String password;
private String nickName;
private Integer isDeleted;
@ApiModelProperty(value="id数组",hidden=true)
private String[] ids;
private List<String> idList;
//省略get/set
}
@ApiOperation("更改用户信息")
@PostMapping("/updateUserInfo")
public int updateUserInfo(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) User user){
int num = userService.updateUserInfo(user);
return num;
}
@ApiIgnore()用于类或者方法上,可以不被swagger显示在页面上
比较简单, 这里不做举例
@ApiImplicitParam() 用于方法
表示单独的请求参数
@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
name–参数ming
value–参数说明
dataType–数据类型
paramType–参数类型
example–举例说明
@ApiOperation("查询测试")
@GetMapping("select")
//@ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query")
@ApiImplicitParams({
@ApiImplicitParam(name="name",value="用户名",dataType="string", paramType = "query",example="xingguo"),
@ApiImplicitParam(name="id",value="用户id",dataType="long", paramType = "query")})
public void select(){
}
本文参考CSDN博主「兴国First」的原创文章
原文链接:https://blog.csdn.net/u014231523/article/details/76522486
Swagger2常用注解解析(轻松构建Swagger)的更多相关文章
- swagger2常用注解
常用注解: @Api()用于类: 表示标识这个类是swagger的资源 @ApiOperation()用于方法: 表示一个http请求的操作 @ApiParam()用于方法,参数,字段说明: 表示对参 ...
- Swagger2常用注解和使用方法
一 引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...
- swagger2常用注解说明
说明: 1.这里使用的版本:springfox-swagger2(2.4)springfox-swagger-ui (2.4) 2.这里是说明常用注解的含义和基本用法(也就是说已经对swagger进行 ...
- swagger2 常用注解的使用
一.@Api 效果: @Api注解放在类上面,这里的value是没用的,tags表示该controller的介绍. 二 .@ApiOperation 效果: @ApiOperation注解用于放在方法 ...
- swagger2 常用注解说明
常用到的注解有: Api ApiModel ApiModelProperty ApiOperation ApiParam ApiResponse ApiResponses ResponseHeader ...
- Swagger2常用注解及其说明 (转)
Api 用在Controller中,标记一个Controller作为swagger的文档资源 属性名称 说明 value Controller的注解 description 对api资源的描述 hid ...
- SpringBoot常用注解解析
@RestController 将返回的对象数据直接以 JSON 或 XML 形式写入 HTTP 响应(Response)中.绝大部分情况下都是直接以 JSON 形式返回给客户端,很少的情况下才会以 ...
- 常用注解解析(因为不太明白@component和@configuration写了)
1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 3.@rep ...
- lombok --- 常用注解解析
@Data@Getter @Setter @ToString@Cleanup@NonNull@Builder@EqualsAndHashCode
随机推荐
- C语言中函数的返回值
规则 除局部变量的内存地址不能作为函数的返回值外,其他类型的局部变量都能作为函数的返回值. 我总结出下面这些规则: int.char等数据类型的局部变量可以作为函数返回值. 在函数中声明的指针可以作为 ...
- python + mysql 实现创建数据表
import pymysql"""1.连接本地数据库2.建立游标3.创建表4.插入表数据.查询表数据.更新表数据.删除表数据"""def c ...
- ThinkPHP3.2.3使用PHPExcel类操作excel导入读取excel
方法一: 1. 下载PHPExcel并保存在如下位置: 2. 在控制器中引用 vendor("PHPExcel.PHPExcel"); $objReader = \PHPExcel ...
- 【Uva1025 A Spy in the Metro】动态规划
题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...
- response 重定向
背景: controller层需要重定向到其他html界面时,需要用如下代码 // 设置302状态码 response.setStatus(302); // 设置location响应头 respons ...
- ODOO14笔记---系统升级崩溃后进不去系统解决办法
一.通过pycharm升级模块: 2.对于已安装odoo模块,升级报错系统崩溃的解决办法:---执行以下SQL update ir_module_module set state ='ins ...
- 解决org.hibernate.LazyInitializationException的正确姿势
项目运行过程中,一个报错信息,报错信息如下: org.hibernate.LazyInitializationException: could not initialize proxy [xxx.do ...
- 【Java经验分享篇01】小白如何开始学会看开源项目?
目录 前言 1.理解开源 1.1.什么是开源? 1.2.开源的定义 1.2.1.开源软件优点 1.2.2.经典开源软件案例 1.3.关于开源协议 1.3.1.如何选择开源协议 2.如何查找开源项目 2 ...
- 群晖NAS网络存储服务器防盗防小偷
群晖NAS网络存储服务器防盗防小偷 根据群晖NAS的实际测量外形尺寸到淘宝网邮购金属 配电箱(弱电箱). 把配电箱(弱电箱)用粗螺丝固定到机柜或墙壁上. 把群晖NAS用密码纯铜挂锁锁在配电箱(弱电箱 ...
- SQL Server存储过程执行一个带返回值(output)的存储过程
语法如下: --存储过程一CREATE PROCEDURE testProc @out VARCHAR(500) OUTPUT AS BEGIN SET @out='操作成功' RETURNEND - ...