文章目录提示:这玩意真的很头疼,由于springboot版本更新太快,容易出现问题,我在这记录下
参考手册:https://doc.xiaominfo.com/knife4j/
- 二、使用步骤
- 1.引入knife4j 接口管理
- 2.spring boot进行配置
- 2.进行Controller测试
- 三、解决方案
- 1.yml增加配置
- 2.运行结果
knife4j是国人开发的一个基于Swagger2技术,为前后端的开发人员的沟通提供便利手段(苦逼后端仔)
二、使用步骤 1.引入knife4j 接口管理pom.xml:
2.spring boot进行配置com.github.xiaoymin knife4j-spring-boot-starter3.0.2
代码如下(示例):
public class Knife4jConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.description("后台接口文档")
.contact(new Contact("admin", "http://127.0.0.1:8080", ""))
.version("v1.0")
.title("接口文档")
.build();
}
}
@Bean:这个Bean对象放在自己的IOC容器中;
2.进行Controller测试
代码如下(示例):
@Api(tags = "测试模块")
@RestController
public class IndexController {
@ApiOperation(value="hello接口",notes ="hello接口的描述")
@GetMapping("/hello")
public ResultVO
我们进行测试时候,会发现惊奇 发现有bug,是关于springfox这叼玩意的错,别怕!!
三、解决方案 1.yml增加配置
application.yml
spring:
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
原因:这是因为Springfox使用的路径匹配是基于AntPathMatcher的, 而Spring Boot 2.6.X使用的是PathPatternMatcher。
ps:knife4J3.0把原来swagger的页面覆盖了,只能打开doc.html,无法打开swagger-ui/index.html
2.运行结果
进入该网站即可:http://localhost:8080/doc.html



