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
随机推荐
- 雪花算法(SnowFlake)Java实现
分布式id生成算法的有很多种,Twitter的SnowFlake就是其中经典的一种. 算法原理 SnowFlake算法生成id的结果是一个64bit大小的整数,它的结构如下图: 1bit,不用,因为二 ...
- vue(23)Vuex的5个核心概念
Vuex的核心概念 Vuex有5个核心概念,分别是State,Getters,mutations,Actions,Modules. State Vuex使用单一状态树,也就是说,用一个对象包含了所 ...
- 【对线面试官】CountDownLatch和CyclicBarrier的区别
<对线面试官>系列目前已经连载31篇啦,这是一个讲人话面试系列 [对线面试官]Java注解 [对线面试官]Java泛型 [对线面试官] Java NIO [对线面试官]Java反射 &am ...
- 如何进行TIDB优化之Grafana(TiDB 3.0)关注监控指标
前言 在对数据库进行优化前,我们先要思考一下数据库系统可能存在的瓶颈所在之外.数据库服务是运行在不同的硬件设备上的,优化即通过参数配置(不考虑应用客户端程序的情况下),而实现硬件资源的最大利用化.那么 ...
- 【codeforces1058】Vasya and Golden Ticket 枚举+暴力+模拟
#点击传送 题目描述 Recently Vasya found a golden ticket - a sequence which consists of nn digits a1a2-ana1a2 ...
- Axure RP 9 Enterprise/Pro/Team for Mac/Windows安装破解版激活教程
Axure RP 9.0 是一款功能强大的.操作方便.专业可靠的快速原型设计工具.一款能够在这里体验最简单的设计方式,这里有着全新的升级的软件界面,更加的时尚,更加的丰富,专为每一个用户提供了便捷的设 ...
- Maven之--安装nexus 私服
开始搜索下载了,nexus3.19版本,下来之后,建立一个maven 骨架过程 quickstart,提示没有lgf4j依赖和和maven插件都没有,开始搜索什么原因,猜想是nexus没有索引,右搜索 ...
- 在 CentOS 7.5 64位上使用 yum 安装 MySQL 8.0
前段时间在 CentOS 7.5 64位上安装 MySQL 8.0.查了些资料,在这里记录一下详细的安装和设置步骤. 一.安装 使用yum安装MySQL之前需要先下载对应的.rpm文件,下载方法: 去 ...
- Spring Cloud专题之五:config
书接上回: SpringCloud专题之一:Eureka Spring Cloud专题之二:OpenFeign Spring Cloud专题之三:Hystrix Spring Cloud 专题之四:Z ...
- leetcode 有效三角形的个数
题目描述: 平明伞兵解法: 既然要求满足三角形要求的三边,简单来说,就是最短两边之和大于第三边,所以,第一步Arrays.sort().先排序,然后直接伞兵暴力法,三重循环.当然最后肯定是能跑出来的, ...