第一步Maven依赖导入
<!-- swagger2 -->
<!-- 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>
第二步启用@EnableSwagger2
注意:使用@EnableSwagger2注解开启Swagger功能, 然后用浏览器访问http://localhost:8080/swagger-ui.html
,若能打开该页面,则开启成功。
@SpringBootApplication
@EnableSwagger2
public class OmallApplication {
public static void main(String[] args) {
SpringApplication.run(OmallApplication.class, args);
}
}
定制SwaggerConfig配置文件
创建SwaggerConfig配置文件,再创建一个Docket()对象并注入Spring容器中,通过设置Docket属性来配置Swagger
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
public ApiInfo apiInfo() {
return new ApiInfo("omall的API文档标题",
"omallAPI的描述信息",
"1.0版本",
"oleolema公司",
new Contact("oleolema姓名", "https://www.github.com/oleolema网站", "oleolema@qq.com邮箱"),
"Apache 2.0开源名称",
"http://www.apache.org/licenses/LICENSE-2.0开源许可地址",
new ArrayList());
}
页面显示结果是这样的
配置需要扫描的接口
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
/*
RequestHandlerSelectors:配置扫描接口的方式
basePackage(String): 扫描指定包
any(): 扫描全部
none(): 不扫描
withClassAnnotation(Class): 扫描类上的注解
withMethodAnnotation(Class): 扫描方法上的注解
*/
.apis(RequestHandlerSelectors.basePackage("com.yqh.omall.controller"))
/*
PathSelectors : 过滤的路径
ant(String) : 正则路径
*/
// .paths(PathSelectors.any())
.build();
}
如果只希望Swagger在开发环境使用
在配置文件中激活开发模式
spring.profiles.active=dev
获取环境并设置enable
@Bean
public Docket docket(Environment environment) {
//获取当前激活环境
boolean dev = environment.acceptsProfiles(Profiles.of("dev"));
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//只在开发模式启用
.enable(dev)
}
分组配置
只需要配置多个docket, 并设置不同的groupName名称
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A组");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B组");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("C组");
}
在网页显示结果是这样的
全部配置
@Configuration
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment) {
boolean dev = environment.acceptsProfiles(Profiles.of("dev"));
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//只在开发模式启用
.enable(dev)
.select()
/*
RequestHandlerSelectors:配置扫描接口的方式
basePackage(String): 扫描指定包
any(): 扫描全部
none(): 不扫描
withClassAnnotation(Class): 扫描类上的注解
withMethodAnnotation(Class): 扫描方法上的注解
*/
.apis(RequestHandlerSelectors.basePackage("com.yqh.omall.controller"))
/*
PathSelectors : 过滤的路径
ant(String) : 正则路径
*/
// .paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo() {
return new ApiInfo("omall的API文档",
"omallAPI的描述信息",
"1.0",
"oleolema",
new Contact("oleolema", "https://www.github.com/oleolema", "oleolema@qq.com"),
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}