1.在pom.xml的依赖中加入:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2.在config文件夹中创建SwaggerConfig写入配置:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean //localhost:8080/swagger-ui.html
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder() //项目描述
.title("演示项目API") //标题
.description("演示项目") //描述
.version("1.0") //版本
.build();
}
}
3.注意事项:
Spring Boot 2.6.X后与Swagger有版本冲突问题,需要在application.properties中加入以下配置:
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
4.完成配置后点击运行访问路径//localhost:8080/swagger-ui.html就可以使用swagger