## 配置pom.xml
在pom.xml中引入
- springfox-boot-starter
- springfox-swagger2
- springfox-swagger-ui
这里使用的版本都为3.0.0
```xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
```
## java配置
```java
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
// 配置扫描指定的包
.apis(RequestHandlerSelectors.basePackage("com.project.controller"))
// 扫描所有包
// .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Bean
public WebMvcConfigurer SwaggerWebMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
};
}
}
```
## 验证成果
http://localhost:8080/your-app-root/swagger-ui/
更多配置请参考 https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
[学习,记录] 在SpringBoot中使用Swagger2