1. Application
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableCaching
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
//配置Swagger信息
private ApiInfo apiInfo() {
return new ApiInfo(
"Api Documentation",
"Swagger API文档",
"1.0",
null,
null,
null,
null,
new ArrayList<VendorExtension>());
}
//配置Swagger的Docket的bean实例
@Bean
public Docket docket(Environment environment) {
//设置要配置的Swagger环境
Profiles profiles = Profiles.of("dev");
// Profiles profiles = Profiles.of("test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//配置Swagger信息
.enable(flag) //通过flag判断是否开启
.select()
/**
* apis():指定扫描的接口
* RequestHandlerSelectors:配置要扫描接口的方式
* basePackage:指定要扫描的包
* any:扫面全部
* none:不扫描
* withClassAnnotation:扫描类上的注解(参数是类上注解的class对象)
* withMethodAnnotation:扫描方法上的注解(参数是方法上的注解的class对象)
*/
.apis(RequestHandlerSelectors.basePackage("com.yt.project.webapp.mgr.controller"))
/**
* paths():过滤路径
* PathSelectors:配置过滤的路径
* any:过滤全部路径
* none:不过滤路径
* ant:过滤指定路径:按照按照Spring的AntPathMatcher提供的match方法进行匹配
* regex:过滤指定路径:按照String的matches方法进行匹配
*/
.paths(PathSelectors.ant("/**"))
.build();
}
@Bean
public UiConfiguration uiConfig() {
return UiConfigurationBuilder.builder()
// .deepLinking(true)
// .displayOperationId(false)
// 隐藏UI上的Models模块
// .defaultModelsExpandDepth(-1)
// .defaultModelExpandDepth(-1)
// .defaultModelRendering(ModelRendering.MODEL)
// .displayRequestDuration(false)
// .docExpansion(DocExpansion.NONE)
// .filter(false)
// .maxDisplayedTags(null)
// .operationsSorter(OperationsSorter.ALPHA)
// .showExtensions(false)
// .tagsSorter(TagsSorter.ALPHA)
// .validatorUrl(null)
.build();
}
}
2.pom添加依耐
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
3. 访问地址: http://localhost:8080/doc.html