- swagger Docket方法中有enable参数(类型为boolean,true表示开启swagger的访问,false表示关闭swagger的访问)
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自定义配置
*/
@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来控制接口是否展示