package com.leyou.config;
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;
/**
* @author 98050
*/
@Configuration
@EnableSwagger2 //开启 swagger
public class SwaggerConfig {
@Bean
public Docket api(Environment environment) {
// 设置要显示swagger的环境
// Profiles of = Profiles.of("dev", "test");
// 判断当前是否处于该环境
// 通过 enable() 接收此参数判断是否要显示
// boolean b = environment.acceptsProfiles(of);
return new Docket(DocumentationType.SWAGGER_2)
.host("http://localhost:8081")
.apiInfo(apiInfo())
.enable(false) //配置是否启用Swagger,如果是false,在浏览器将无法访问
.select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
//指定要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.leyou.controller"))
// 配置如何通过path过滤 paths(PathSelectors.ant("/shashen/**"))扫描shashen下的全部方法
// any()扫描所有,项目中的所有接口都会被扫描到
//none() 不扫描接口
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("商城订单系统") // 头部信息
.description("杀神") //可以写作者
.version("1.0") //自定义版本
.termsOfServiceUrl("https://gitee.com/") //想要显示的网址
.build();
}
}
```java
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
swagger
最新推荐文章于 2024-09-05 10:39:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)