springboot整合swagger2并实现自定义开关

pom.xm文件添加
<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.2.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.2.2</version>
		</dependency>

application类同级目录新建Swagger2类,

import org.springframework.beans.factory.annotation.Value;
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.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * swagger自定义配置
 * 
 * @ClassName: Swagger2
 * @author Administrator
 * @date 2018年4月18日
 */
@Configuration
@EnableSwagger2
public class Swagger2
{
    @Value("${swagger.show}")
    private boolean swaggerShow;

    @Bean
    public Docket createRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xrq.demo")) //swagger扫描指定包下面的接口
                .paths(PathSelectors.any()).build();
    }
    
    private ApiInfo apiInfo()
    {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("此API提供接口调用")
                .contact("xingrenqiang")
                .version("1.0").build();
    }
}

application配置文件添加swagger.show来控制接口暴露的开关

swagger.show=true 或者swagger.show=false来控制接口是否展示

猜你喜欢

转载自blog.csdn.net/xrq0508/article/details/79986576