Bootstrap

springfox-swagger-ui 3.0.0 配置

在3.0中,访问地址URL变了。

  • http://地址:端口/项目名/swagger-ui/

SpringBoot

maven项目引入

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

gradle项目引入

//https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'
//https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '3.0.0'
//https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter
implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'

SwaggerConfig.java 配置

import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
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.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spi.service.contexts.SecurityContextBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2 // 开启Swagger支持
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 将api的元信息设置为包含在json resourcelisting响应中
                .apiInfo(apiInfo()).enable(true) // 是否启用Swagger,如果为false,则swagger不能在浏览器中访问
                .select()
                //加了ApiOperation注解的类,才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类,才生成接口文档
                .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.xxx.controller"))
                .paths(PathSelectors.any())
                .build().securityContexts(securityContexts()).securitySchemes(securitySchemes());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("Apaas平台", "https://www.ctapaas.cn/", "[email protected]");
        return new ApiInfoBuilder().contact(contact).title("Apaas组件").description("Apaas组件Restful Api文档").version("1.0.0").build();
    }

    // 设置需要的登录认证的路径
    private List<SecurityContext> securityContexts() {
        SecurityContextBuilder builder = SecurityContext.builder();
        builder.securityReferences(defaultAuth()).operationSelector(oc->oc.requestMappingPattern().matches("/.*"));
        return Collections.singletonList(builder.build());
    }

    // 设置请求头的信息
    private List<SecurityScheme> securitySchemes() {
        return Collections.singletonList(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));
    }

    // 定义授权范围
    private List<SecurityReference> defaultAuth() {
        List<AuthorizationScope> scopeList = Collections.singletonList(new AuthorizationScope("global", "全局认证"));
        return Collections.singletonList(new SecurityReference("Authorization", scopeList.toArray(new AuthorizationScope[0])));
    }
}

这是效果图

在这里插入图片描述

这是官方说明文档
https://springfox.github.io/springfox/docs/snapshot/#changes-in-swagger-ui

如果帮助到了大家,点个赞或者来个关注。

;