0
点赞
收藏
分享

微信扫一扫

Swagger2配置

罗子僧 2022-03-15 阅读 61

Swagger2配置

maven

 		<!-- swagger2 依赖 接口文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- Swagger第三方ui依赖 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    // 扫描有哪些包下生成 Swagger文档
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.abin.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("云e办接口文档")
                .description("云e办接口文档")
                .contact(new Contact("crabin" , "http://localhost:8088/doc.html","1528392308@qq.com"))
                .version("1.0")
                .build();

    }
}

导入版本正确可以,加入报错:Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is

可能是因为Springboot版本太高导致的

两种解决方法:

1.编写 WebMvcConfigurer 配置类


@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {

    /**
     * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}

2.在 application.yaml 中:

spring:
 mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

访问:http://localhost:8088/doc.html

调试:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pAmOgnSd-1647345160640)(Swagger2%E9%85%8D%E7%BD%AE.assets/1647333585876.png)]

在createRestApi()方法后面继续 设置权限:

.securityContexts(securityContexts())

.securitySchemes(securitySchemes());


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    // 扫描有哪些包下生成 Swagger文档
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.abin.controller"))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(securityContexts())	
                .securitySchemes(securitySchemes());
    }

    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("云e办接口文档")
                .description("云e办接口文档")
                .contact(new Contact("crabin" , "http://localhost:8088/doc.html","1528392308@qq.com"))
                .version("1.0")
                .build();
    }

    private List<ApiKey> securitySchemes() {
        // 设置请求头
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "Header");
        result.add(apiKey);
        return result;
    }

    private List<SecurityContext> securityContexts() {
        // 设置需要登录认证的路径
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/hello/.*"));
        return result;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }
}

设置权限:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PEKc8unM-1647345160643)(Swagger2%E9%85%8D%E7%BD%AE.assets/1647333507942.png)]

result.add(new SecurityReference(“Authorization”, authorizationScopes));
return result;
}
}


设置权限:[外链图片转存中...(img-PEKc8unM-1647345160643)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4OPsgQB9-1647345160644)(Swagger2%E9%85%8D%E7%BD%AE.assets/1647333483631.png)]
举报

相关推荐

0 条评论