Swagger系列——Swagger3配置
Swagger系列——Swagger3配置
- 什么是 Swagger?
- 依赖引入
- springfox引入方式
- knife4j引入方式
- 引入美化bootstrap-UI
- 详细配置
- swagger资源访问路径加入白名单
- 访问路径:
- 注意:
相关内容 | 地址 |
---|---|
Swagger官方文档 | https://swagger.io/docs/specification/2-0/basic-structure/ |
Swagger常用注解 | https://blog.csdn.net/weixin_42526326/article/details/119824857 |
Swagger系列——Swagger2配置 | https://blog.csdn.net/weixin_42526326/article/details/119963866 |
Swagger系列——Swagger3配置 | https://blog.csdn.net/weixin_42526326/article/details/119965092 |
什么是 Swagger?
Swagger是一组围绕 OpenAPI 规范构建的开源工具,可帮助您设计、构建、记录和使用 REST API。主要的 Swagger 工具包括:
Swagger Editor – 基于浏览器的编辑器,您可以在其中编写 OpenAPI 规范。
Swagger UI – 将 OpenAPI 规范呈现为交互式 API 文档。
swagger2于17年停止维护,现在最新的版本为 Swagger3(Open Api3)。
依赖引入
我们可以去 mvnrepository(maven中央仓库) 搜索版本,但是建议不要用最新版
springfox引入方式
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version>
</dependency>
knife4j引入方式
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency>
引入美化bootstrap-UI
<!-- 引入swagger-bootstrap-ui包 -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.8.5</version>
</dependency>
详细配置
配置文件加上@EnableOpenApi、@Configuration 会自动开启配置,启动类不需要加任何注解
package com.doctorcloud.pc.interceptor.config;import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;/**** @author */
@Configuration
@Profile({"dev","local"})
@EnableOpenApi
@EnableSwaggerBootstrapUI
public class SwaggerConfig {/*** 是否开启swagger配置,生产环境需关闭*/
/* @Value("${swagger.enabled}")*/private boolean enable;/*** 创建API* http:IP:端口号/swagger-ui/index.html 原生地址* http:IP:端口号/doc.html bootStrap-UI地址*/@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.OAS_30).pathMapping("/")// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)/*.enable(enable)*/.apiInfo(apiInfo())// 设置哪些接口暴露给Swagger展示.select()// 扫描所有有注解的api,用这种方式更灵活.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))// 扫描指定包中的swagger注解// .apis(RequestHandlerSelectors.basePackage("com.doctorcloud.product.web.controller"))// 扫描所有 .apis(RequestHandlerSelectors.any()).paths(PathSelectors.regex("(?!/ApiError.*).*")).paths(PathSelectors.any()).build()// 支持的通讯协议集合.protocols(newHashSet("https", "http")).securitySchemes(securitySchemes()).securityContexts(securityContexts());}/*** 支持的通讯协议集合* @param type1* @param type2* @return*/private Set<String> newHashSet(String type1, String type2){Set<String> set = new HashSet<>();set.add(type1);set.add(type2);return set;}/*** 认证的安全上下文*/private List<SecurityScheme> securitySchemes() {List<SecurityScheme> securitySchemes = new ArrayList<>();securitySchemes.add(new ApiKey("token", "token", "header"));return securitySchemes;}/*** 授权信息全局应用*/private List<SecurityContext> securityContexts() {List<SecurityContext> securityContexts = new ArrayList<>();securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build());return securityContexts;}private List<SecurityReference> defaultAuth() {AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];authorizationScopes[0] = authorizationScope;List<SecurityReference> securityReferences = new ArrayList<>();securityReferences.add(new SecurityReference("Authorization", authorizationScopes));return securityReferences;}/*** 添加摘要信息*/private ApiInfo apiInfo() {// 用ApiInfoBuilder进行定制return new ApiInfoBuilder()// 设置标题.title("接口文档")// 描述.description("描述")// 作者信息.contact(new Contact("doctorCloud", null, null))// 版本.version("版本号:V.1")//协议.license("The Apache License")//协议url.licenseUrl("http://www.baidu.com").build();}
}
swagger资源访问路径加入白名单
- /swagger-ui/**
- /webjars/**
- /doc.html
- /swagger-resources/**
- /v3/**
访问路径:
http:IP:端口号/swagger-ui/index.html 原生地址
http:IP:端口号/doc.html bootStrap-UI地址
注意:
不建议使用swagger原生页面设置权限,建议使用doc页面设置token,搜索接口更方便(主要是好看)
之前有用swagger3,突然间接口文档出参都显示不了,不得已只得退回到swagger2
Swagger系列——Swagger3配置相关推荐
- Swagger3配置
使用Swagger3去简化以前Swagger2的配置 引入依赖 <!-- swagger --><dependency><groupId>io.springfox& ...
- LOAM系列——LeGO-LOAM配置、安装、问题解决及VLP16测试效果(完结版)
LOAM系列--LeGO-LOAM配置.安装.问题解决及VLP16测试效果 安装依赖 安装 VLP16 bag测试 问题解决 问题1 解决1 安装依赖 ros gtsam wget -O ~/Down ...
- LOAM系列——FLOAM配置、安装、问题解决及VLP16测试效果(完结版)
LOAM系列--FLOAM配置.安装.问题解决及VLP16测试效果 安装依赖 安装 KITTI sequence 07 VLP16 bag测试 问题解决 问题1 安装依赖 Ubuntu and ROS ...
- RS485modbus转Profinet网关协议连接富凌DZB300系列变频器配置方法
RS485modbus转Profinet网关协议连接富凌DZB300系列变频器配置方法 案例介绍:改造项目原系统的1200plc连接了多台富凌DXB300系列变频器,出现干扰导致间断性变频器报警,重启 ...
- 华为S5700系列交换机配置
华为S5700系列交换机配置 vlan ba 9 11 93 //批量创建vlan int vlan 93 //进入三层接口 ip add 10.93.1.115 24 //配置三层网关地址 des ...
- Kubernetes 1.6新特性系列 | 动态配置和存储类
导读: Dynamic Provisioning的目标是完全自动化存储资源的生命周期管理,让用户无需过多的关注存储的管理,可以按需求自动动态创建和调整存储资源.StorageClass本质上是底层存储 ...
- proe指定服务器安装,ucs-c系列服务器安装配置-v1
<ucs-c系列服务器安装配置-v1>由会员分享,可在线阅读,更多相关<ucs-c系列服务器安装配置-v1(33页珍藏版)>请在人人文库网上搜索. 1.安装和配置C系列服务器详 ...
- 华为ar2200series配置手册_思科ASA系列防火墙配置手册
思科ASA系列防火墙配置手册 思科ASA系列防火墙配置手册 使用console连接线登录方法 1.使用cisco的console连接线,连接设备的console口和计算机com口(笔记本用USB转CO ...
- opporeno5可以用鸿蒙系统,opporeno5系列参数配置_oppo reno5手机参数
来到了2020年的最后一个月,在这个月中会有不少新机上市,其中OPPO有一个OPPO Reno5系列将在12月10日正式发布,据了解这个系列有两款手机分别为OPPO Reno5和OPPO Reno5 ...
最新文章
- 《.NET应用架构设计:原则、模式与实践》新书博客--试读-1.3 架构设计中的重要概念...
- css 设置table样式
- Java 最多能买到的笔数
- webpack html转成js,WebPack的基础学习
- 实战-130W表增加字段耗时
- LinkedList剖析
- 前端学习(2740):重读vue电商网站50之Element-UI 组件按需加载
- 常用内存分配函数的说明
- debian 安装java_debian9安装jdk1.8
- 在asp.net2.0中使用存储过程
- 推荐一个CSLab------英真时代(非广告,真心的)
- 记录这两天所学的东西
- 华锐研究:国际领先经营机构(投行)信息技术投入 | 数据发布
- 关于表单流程设计器 以及 问卷调查生成器的开发准备
- caffe 6中优化方法并附带 对应的solver。prototxt代码
- 今天开通个人博客,值得祝贺!
- 《UNIX/LINUX系统管理I》课程学习总结
- 计算机里的word怎么重装,word能卸载重装吗 word卸载重装
- 2022最新网络安全零基础学习路线
- jquery日期选择插件