首页 > 技术文章 > Swagger

GGhot 2021-04-23 22:56 原文

笔记

spring-boot整合丝袜哥

 

1.导入依赖

 

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

 

 

2.配置Swagger

@Configuration
@EnableSwagger2 //启用Swagger2
public class SwaggerConfig {
}

 

3.访问页面http://localhost:8080/swagger-ui.html

 

 

4.配置swagger信息

 Swagger的bean实例Docket
@Configuration
@EnableSwagger2 //启用Swagger2
public class SwaggerConfig {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    //配置Swagger信息apiInfo
//    @Bean
    private ApiInfo apiInfo(){
        Contact contact = new Contact("gg", "https://gitee.com/gg-study/ssm", "xxxxx@qq.com");
        return new ApiInfo("SwaggerAPI文档", "描述信息", "1.0", "https://gitee.com/gg-study/ssm",
                contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

 

 

Swagger配置

 

 1.配置扫描接口

 

@Bean
public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //RequestHandlerSelectors:配置要扫描的接口
            //basePackage:指定要扫描的包
            //any:扫描全部
            //none:不扫描
            //withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
            //withMethodAnnotation:扫描方法上的注解
            .apis(RequestHandlerSelectors.basePackage("com.gg.swagger.controller"))
            //过滤
            .paths(PathSelectors.ant("/gg/**"))
            .build();
}

2.配置是否启动Swagger

@Bean
public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            //是否使用swagger
            .enable(false)
            .select()

                   

推荐阅读