当前位置: 首页 > 后端技术 > Java

Swagger笔记—Swagger3配置

时间:2023-04-02 02:08:02 Java

@TOC相关内容地址Swagger官方文档https://swagger.io/docs/specification/2-0/basic-structure/Swagger常用注解https://blog.csdn.net/weixin_42526326/article/details/119824857Swagger2常用注解https://blog.csdn.net/weixin_42526326/article/details/119963866Swagger3常用注解https://blog.csdn.net/weixin_42526326/article/details/119965092什么是Swagger?Swagger是一组围绕OpenAPI规范构建的开源工具,可帮助您设计、构建、记录和使用RESTAPI。主要的Swagger工具有:SwaggerEditor-基于浏览器的编辑器,您可以在其中编写OpenAPI规范。SwaggerUI-将OpenAPI规范呈现为交互式API文档.swagger2在2017年停止维护,现在最新版本是Swagger3(OpenApi3)。我们可以去mvnrepository(maven中央仓库)搜索版本进行依赖导入,但是建议不要使用最新版本的springfox导入方式io.springfoxspringfox-boot-starter3.0.0knife4j引入方法com.github.xiaoyminknife4j-spring-boot-starter3.0.3引入美化bootstrap-UIcom.github.xiaoyminswagger-bootstrap-ui1.8.5详细配置配置文件加上@EnableOpenApi,@Configuration会自动开启配置,启动类不需要添加任何注释包com.doctorcloud.pc.interceptor。配置;导入com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;导入io.swagger.annotations.ApiOperation;导入org.springframework.context.annotation.Bean;导入org.springframework.context.annotation.Configuration;导入org.springframework.context.annotation.Profile;导入springfox.documentation.builders.ApiInfoBuilder;导入springfox.documentation.builders.PathSelectors;导入springfox.documentation.builders.RequestHandlerSelectors;导入springfox.documentation.oas.annotations.EnableOpenApi;导入springfox.documentation.service.*;导入springfox.documentation.spi.DocumentationType;导入springfox.documentation.spi.service.contexts.SecurityContext;导入springfox.documentation.spring.web.plugins.Docket;导入springfox.documentation.swagger2.annotations.EnableSwagger2;导入java.util.ArrayList;导入java.util.HashSet;导入java.util.List;导入java.util.Set;/****@author*/@Configuration@Profile({"dev","local"})@EnableOpenApi@EnableSwaggerBootstrapUIpublicclassSwaggerConfig{/***是否开启swagger配置,生产环境需要关闭*//*@值(“${swagger.enabled}”)*/私有布尔启用;/***创建API*http:IP:端口号/swagger-ui/index.html原生地址*http:IP:端口号/doc.htmlbootStrap-UI地址*/@BeanpublicDocketcreateRestApi(){returnnewDocket(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());}/***支持的通信协议集*@paramtype1*@paramtype2*@return*/privateSetnewHashSet(Stringtype1,Stringtype2){Setset=newHashSet<>();set.add(type1);set.add(type2);returnset;}/***认证安全上下文*/privateListsecuritySchemes(){ListsecuritySchemes=newArrayList<>();securitySchemes.add(newApiKey("token","token","header"));returnsecuritySchemes;}/***全局应用授权信息*/privateListsecurityContexts(){ListsecurityContexts=newArrayList<>();securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build());返回安全上下文;}privateListdefaultAuth(){AuthorizationScopeauthorizationScope=newAuthorizationScope("global","accessEverything");AuthorizationScope[]authorizationScopes=newAuthorizationScope[AuthorizationScope[]1];authorizationScopes[0]=authorizationScope;ListsecurityReferences=newArrayList<>();securityReferences.add(newSecurityReference("Authorization",authorizationScopes));返回安全参考;}/***添加摘要信息*/privateApiInfoapiInfo(){//使用ApiInfoBuilder自定义returnnewApiInfoBuilder()//Settitle.title("InterfaceDocument")//Description.description("Description")//作者信息.contact(newContact("doctorCloud",null,null))//version.version("版本号:V.1")//Protocol.license("TheApacheLicense")//协议url.licenseUrl("http://www.baidu.com").build();}}添加swagger资源访问路径到白名单-/swagger-ui/**-/webjars/**-/doc.html-/swagger-resources/**-/v3/**访问路径:http:IP:portnumber/swagger-ui/index.html原文地址http:IP:portnumber/doc.htmlbootStrap-UI地址提示:不建议使用swagger原生页面设置权限,建议使用doc设置token的页面,搜索界面比较方便(主要是好看)之前用的是swagger3,突然界面文档引用不到显示不出来,只好返回swagger