• SpringBoot整合knife4j


    1. 导包

    SpringBoot版本:2.3.7.RELEASE

    		<dependency>
                <groupId>com.github.xiaoymingroupId>
                <artifactId>knife4j-spring-boot-starterartifactId>
                <version>3.0.3version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2. 配置

    import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    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.Collections;
    import java.util.List;
    
    @Configuration
    @EnableSwagger2
    @EnableKnife4j
    @Import(BeanValidatorPluginsConfiguration.class)
    public class Swagger2Config implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    
       
        @Bean(value = "defaultApi2")
        public Docket defaultApi2() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    //此包路径下的类,才生成接口文档
                    .apis(RequestHandlerSelectors.basePackage("com.hebcph"))
                    //加了ApiOperation注解的类,才生成接口文档
                    .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                    .build()
                    .securitySchemes(Collections.singletonList(securityScheme()))
                    .securityContexts(securityContexts());
                    //.globalOperationParameters(setHeaderToken());
        }
    
        
        @Bean
        SecurityScheme securityScheme() {
            return new ApiKey("X-ACCESS-TOKEN", "X-ACCESS-TOKEN", "header");
        }
       
        private List<Parameter> setHeaderToken() {
            ParameterBuilder tokenPar = new ParameterBuilder();
            List<Parameter> pars = new ArrayList<>();
            tokenPar.name("X-ACCESS-TOKEN").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
            pars.add(tokenPar.build());
            return pars;
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    // //大标题
                    .title("后台服务API接口文档")
                    // 版本号
                    .version("1.0")
    //				.termsOfServiceUrl("NO terms of service")
                    // 描述
                    .description("后台API接口")
                    .license("The Apache License, Version 2.0")
                    .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                    .build();
        }
    
        private List<SecurityContext> securityContexts() {
            return new ArrayList(
                    Collections.singleton(SecurityContext.builder()
                            .securityReferences(defaultAuth())
                            .forPaths(PathSelectors.regex("^(?!auth).*$"))
                            .build())
            );
        }
    
        private List<SecurityReference> defaultAuth() {
            AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
            AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
            authorizationScopes[0] = authorizationScope;
            return new ArrayList(
                    Collections.singleton(new SecurityReference("X-ACCESS-TOKEN", authorizationScopes)));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    3. 启动

    http://localhost:8080/doc.html

    tp

  • 相关阅读:
    学会可视化大屏布局技巧,让领导都赞不绝口
    SpringBoot项目整合Vue做一个完整的用户注册功能
    【第29例】IPD体系进阶:PL-TMT 产品线技术管理团队
    Thread常用API
    状态压缩学习笔记
    Java之异常浅析
    idea显示git分支信息(GitToolBox插件)
    PDE数值解中,为什么要引入弱解(weak solution)的概念?
    通用场景图像分割
    力扣(518.377)补7.31
  • 原文地址:https://blog.csdn.net/weixin_45947759/article/details/134015884