1、在pom文件中添加依赖
<!-- 添加swapper依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency><dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version></dependency>
2、创建swagger的配置文件SwaggerConfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){//这里可以根据自己的需要取名
return new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfoBuilder().title("支付接口测试").build());
}
}
3、启动项目(可能会报下面错误)
4、在application.properties中添加如下
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
原因:因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
解决:在application.properties里配置
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
5、访问http://localhost:port/swagger-ui.html (其中localhost为ip,port为端口号)