这个错误是在集成Swagger时出现的。Spring Boot 2.4.x以上版本默认的路径匹配策略是PathPatternParser,而Springfox假设Spring MVC的路径匹配策略是AntPathMatcher,这就导致了错误。下面是解决此问题的方法:
- 在application.properties文件中添加以下配置,将路径匹配策略更改为AntPathMatcher:
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
- 在pom.xml中排除spring-boot-starter-web依赖并添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
这会将Spring Boot的默认嵌入式Tomcat容器替换为Jetty容器,因为Jetty使用AntPathMatcher作为默认的路径匹配策略。
不过使用第一种方法可能会导致无法使用Spring Actuator,因为它使用PathPattern作为URL匹配策略。在这种情况下,可以尝试使用第二种方法,即将Tomcat替换为Jetty容器。