「尝鲜」SpringBoot 快速整合 Swagger3.0

第一步:Maven引入Swagger3.0 starter依赖Maven项目中引入springfox-boot-starter依赖:
io.springfoxspringfox-boot-starter3.0.0备注:依赖更加简单 , 不再需要springfox-swagger2 和 springfox-swagger-ui 这两个依赖 。
第二步:增加application.yml配置swagger:enable: trueapplication-name: ${spring.application.name}application-version: 1.0application-description: springfox swagger 3.0整合Demotry-host: http://localhost:${server.port}spring:application:name: springfox-swagger第三步:启用swagger配置【「尝鲜」SpringBoot 快速整合 Swagger3.0】使用@EnableOpenApi注解 , 启用swagger配置
@EnableOpenApi@Configurationpublic class SwaggerConfiguratio {}第四步:启动测试访问地址:
「尝鲜」SpringBoot 快速整合 Swagger3.0文章插图
备注:老版本的访问方式为 已经不可用了 。
扩展:完整的springfox swagger配置示例import io.swagger.models.auth.In;import org.apache.commons.lang3.reflect.FieldUtils;import org.springframework.boot.SpringBootVersion;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.util.ReflectionUtils;import org.springframework.web.servlet.config.annotation.InterceptorRegistration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;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 java.lang.reflect.Field;import java.util.*;@EnableOpenApi@Configurationpublic class SwaggerConfiguration implements WebMvcConfigurer {private final SwaggerProperties swaggerProperties;public SwaggerConfiguration(SwaggerProperties swaggerProperties) {this.swaggerProperties = swaggerProperties;}@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.OAS_30).pathMapping("/")// 定义是否开启swagger , false为关闭 , 可以通过变量控制.enable(swaggerProperties.getEnable())// 将api的元信息设置为包含在json ResourceListing响应中 。.apiInfo(apiInfo())// 接口调试地址.host(swaggerProperties.getTryHost())// 选择哪些接口作为swagger的doc发布.select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build()// 支持的通讯协议集合.protocols(newHashSet("https", "http"))// 授权信息设置 , 必要的header token等认证信息.securitySchemes(securitySchemes())// 授权信息全局应用.securityContexts(securityContexts());}/*** API 页面上半部分展示信息*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title(swaggerProperties.getApplicationName() + " Api Doc").description(swaggerProperties.getApplicationDescription()).contact(new Contact("lighter", null, "123456@gmail.com")).version("Application Version: " + swaggerProperties.getApplicationVersion() + ", Spring Boot Version: " + SpringBootVersion.getVersion()).build();}/*** 设置授权信息*/private List securitySchemes() {ApiKey apiKey = new ApiKey("BASE_TOKEN", "token", In.HEADER.toValue());return Collections.singletonList(apiKey);}/*** 授权信息全局应用*/private List securityContexts() {return Collections.singletonList(SecurityContext.builder().securityReferences(Collections.singletonList(new SecurityReference("BASE_TOKEN", new AuthorizationScope[]{new AuthorizationScope("global", "")}))).build());}@SafeVarargsprivate finalSet newHashSet(T... ts) {if (ts.length > 0) {return new LinkedHashSet