一. 引言

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。

总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。

二. SpringBoot集成Swagger

2.1 创建SpringBoot项目

  • 正常创建即可

2.2 导入依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

2.3 编写一个hello工程

  • SwaggerController.java
  • 运行启动类, 测试是否可以正常运行
package com.dz.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class SwaggerController { @RequestMapping("/hello")
public String hello(){
return "hello swagger";
}
}

2.4 配置Swagger

  • config目录下新建SwaggerConfig
package com.dz.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig { }

2.5 测试

  • 访问http://localhost:8080/swagger-ui.html

三. 配置Swagger

  • Swagger的bean实例Docket

3.1 配置Swagger信息

  • Docket.apiInfo()
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig { @Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
} //配置Swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("dz","https://www.cnblogs.com/MRASdoubleZ/","java137@163.com");
return new ApiInfo(
"dz的SwaggerAPI文档",
"学无止境",
"1.0",
"https://www.cnblogs.com/MRASdoubleZ/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()); } }

3.2. Swagger配置扫描接口

  • Docket.select()
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig { @Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//RequestHandlerSelectors: 配置要扫描接口的方式
//basePackage: 指定要扫描的包
//any(): 扫描全部
//none(): 不扫描
//withClassAnnotation: 扫描类上的注解, 参数是一个注解的反射对象
//withMethodAnnotation: 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.dz.controller"))
//paths(): 过滤什么路径
.paths(PathSelectors.ant("/dz/**"))
.build();
} }

3.3 配置是否启用Swagger

  • Docket.enable(false), 表示不启用Swagger
//配置了Swagger的Docket的bean实例
//enable(false), 表示不启用Swagger
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.dz.controller"))
//.paths(PathSelectors.ant("/dz/**"))
.build();
}
  • 在开发环境(dev)和测试环境(test)中启用Swagger, 在发布环境(pro)中不启用Swagger
  • Profiles profiles = Profiles.of("dev","test");
  • boolean flag = environment.acceptsProfiles(profiles);
  • Docket.enable(flag)
//配置了Swagger的Docket的bean实例
//enable(false), 表示不启用Swagger
@Bean
public Docket docket(Environment environment){ //设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.dz.controller"))
//.paths(PathSelectors.ant("/dz/**"))
.build();
}

3.4 配置Swagger的分组

  • Docket.groupName("dz")
@Bean
public Docket docket(Environment environment){ //设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("dz")
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.dz.controller"))
//.paths(PathSelectors.ant("/dz/**"))
.build();
}
  • 配置多个分组

    • 多个Docket实例即可
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("dz1");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("dz2");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("dz3");
}

3.5. Api注释

3.5.1 实体类
  • User
package com.dz.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel("用户实体类")
public class User { @ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
3.5.2 Controller
package com.dz.controller;

import com.dz.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class SwaggerController { @GetMapping("/hello")
public String hello(){
return "hello swagger";
} //只要我们的接口中, 返回值中存在实体类, 就会被扫描到Swagger中
@PostMapping("/user")
public User user(){
return new User();
} //@ApiOperation不是放在类上的, 而是方法上
@ApiOperation("Hello控制类")
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String username){
return "hello" + username;
} @ApiOperation("Post测试类")
@PostMapping("/p")
public User p(@ApiParam("用户名") User user){
int i = 5/0;
return user;
}
}
  • 总结:

    1. 我们可以通过Swagger给一些比较难理解的属性或接口增加注释信息
    2. 接口文档实时更新
    3. 可以在线测试
  • 注意: 在正式发布的时候, 关闭Swagger!!! 出于安全考虑, 而且节省运行的内存

11_Swagger的更多相关文章

随机推荐

  1. APISpace 未来7天生活指数API接口 免费好用

    随着经济的发展,我们的生活水平在不断的提高,生活指数在我们的生活中也越来越受到关注,根据当天的生活指数,我们就可以知道在今天我们可以干什么比较好.   未来7天生活指数API,支持国内3400+个城市 ...

  2. [原创]树莓派CM4配置GPIO复用为i2c

    1.简介 项目中需要控制各种外设的电源,正常应该是通过GPIO进行控制,但是树莓派CM4的GPIO管脚有限,因此需要使用i2c扩展IO 查阅CM4-datesheet发现GPIO22和GPIO23可以 ...

  3. Solution -「SDOI2011」拦截导弹

    Sol.   题目要求一个数对序列的二维最长下降子序列,我们称其为 Q.并求出每一个元素分别在可能的 Q 中出现了多少次.   直接 Dp,时间复杂度 \(O(n^2)\) 不行.考虑 CDQ 分治 ...

  4. 算法竞赛进阶指南0x14 Hash

    组成部分: 哈希函数: 链表 AcWing137. 雪花雪花雪花 因为所需要数据量过于大,所以只能以O(n)的复杂度. 所以不可能在实现的过程中一一顺时针逆时针进行比较,所以采用一种合适的数据结构. ...

  5. 只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)

    前言 之前我们分享过基于echarts 的数据可视化展示,很多朋友就说,不会软件开发,可不可以直接用Excel进行数据化的展示. 答案是肯定的,确实有这种方案,百度查询一查一大推,各种解决方案各种模板 ...

  6. Selenium自动化测试之Selenium IDE

    简介 Selenium IDE 是实现Web自动化的一种便捷工具,本质上它是一种浏览器插件.该插件支持Chrome和Firefox浏览器,拥有录制.编写及回放操作等功能,能够快速实现Web的自动化测试 ...

  7. mysql 跨库事务XA

    前一段时间在工作中遇到了跨库事务问题,后来在网上查询了一下,现在做一下整理和总结. 1.首先要确保mysql开启XA事务支持 SHOW VARIABLES LIKE '%XA%' 如果innodb_s ...

  8. 【原创】Python 极验滑块验证

    本文仅供学习交流使用,如侵立删! 记一次 极验滑块验证分析并通过 操作环境 win10 . mac Python3.9 selenium.seleniumwire 分析 最近在做的一个项目登录时会触发 ...

  9. PhpStorm 中文设置教程

    本文仅供学习交流使用,如侵立删!demo下载见文末 Pycharm中文设置教程 1.首先打开PhpStorm ,点击file-settings.找到plugins,搜索Marketplace,然后搜索 ...

  10. 浅谈 Raft 分布式一致性协议|图解 Raft

    前言 本篇文章将模拟一个KV数据读写服务,从提供单一节点读写服务,到结合分布式一致性协议(Raft)后,逐步扩展为一个分布式的,满足一致性读写需求的读写服务的过程. 其中将配合引入Raft协议的种种概 ...