SpringFox 3.0.0——配置类DEMO

Maven

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

 

源代码 

application.properties

spring.application.name=DEMO
spring.application.version=1.0
spring.application.description=Demo project for Spring Boot
nature.restApi.enabled=true

SpringFox 3.0.0 配置类

package cn.edu.zstu.shihua.xihu.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootVersion;
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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
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.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;

/**
 * @Author ShenTuZhiGang
 * @Version 1.0.0
 * @Date 2020-04-13 19:57
 * Swagger2_3 配置类
 */
@Configuration
@EnableSwagger2
@EnableOpenApi
public class CustomSwagger2_3Config {

    /**
     * 是否开启swagger,生产环境一般关闭,所以这里定义一个变量
     */
    @Value("${nature.restApi.enabled}")
    private Boolean enable;

    /**
     * 项目应用名
     */
    @Value("${spring.application.name}")
    private String applicationName;

    /**
     * 项目版本信息
     */
    @Value("${spring.application.version}")
    private String applicationVersion;

    /**
     * 项目描述信息
     */
    @Value("${spring.application.description}")
    private String applicationDescription;

    @Bean
    Docket docket(){
        return new Docket(DocumentationType.OAS_30)
                //将servlet路径映射(如果有)添加到apis基本路径
                .pathMapping("/demo")
                // 定义是否开启swagger,false为关闭,可以通过变量控制
                .enable(enable)
                .select()
                //配置需要扫描的controller位置
                .apis(RequestHandlerSelectors
                        .basePackage("cn.edu.controller"))
                //配置路径
                .paths(PathSelectors.any())
                //构建
                .build()
                //文档信息
                .apiInfo(new ApiInfoBuilder()
                    //标题
                    .title(applicationName + " RESTful API 文档")
                    //版本
                    .version("Application Version: " + applicationVersion + ", " +
                            "Spring Boot Version: " + SpringBootVersion.getVersion())
                    //描述
                    .description(applicationDescription)
                    //联系人信息
                    .contact(new Contact("申屠志刚",//名字
                            "https://shentuzhigang.blog.csdn.net/",//网址
                            "1@qq.com"))//邮箱
                    //License
                    .license("Apache 2.0")
                    //License 网址
                    .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                    .build())

                // 支持的通讯协议集合
                .protocols(new LinkedHashSet<>(
                        Arrays.asList("https", "http")))

                // 授权信息设置,必要的header token等认证信息
                .securitySchemes(Collections.singletonList(
                        new ApiKey("BASE_TOKEN", "token", "pass")))

                // 授权信息全局应用
                .securityContexts(Collections.singletonList(
                        SecurityContext
                                .builder()
                                .securityReferences(Collections
                                        .singletonList(
                                                new SecurityReference("BASE_TOKEN",
                                                        new AuthorizationScope[] {
                                                                new AuthorizationScope("global", "")
                                                    }
                                                )
                                        )
                                )
                                .build()
                    )
                );
    }
}

运行效果

常见问题

SpringFox 3.0.0(包含springfox-swagger2-3.0.0)——无法访问/swagger-ui.html解决方案 

参考文章

https://blog.csdn.net/lilinhai548/article/details/107394670

 

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页