Spring Boot 3.x版本下HelloWorld接口返回404问题求助
Spring Boot 3.x 根路径404问题解决方法
问题原因
Spring Boot 3.x 依赖的Spring Framework 6 默认将路径匹配策略从AntPathMatcher切换成了PathPatternParser,新策略下,@GetMapping(path = "")不再匹配根路径/,这就是访问localhost:8080返回404的原因。
解决方法
方法一:修改请求映射路径
直接把@GetMapping的路径改成"/",修改后的代码如下:
@SpringBootApplication @RestController public class HelloworldApplication { public static void main(String[] args) { SpringApplication.run(HelloworldApplication.class, args); } @GetMapping(path = "/") public ResponseEntity<String> sayHello() { return new ResponseEntity<>("Hello world!", HttpStatus.OK); } }
方法二:切换回旧的路径匹配策略
如果不想修改代码,可以在配置文件中添加配置,切换回原来的AntPathMatcher:
若使用application.properties:
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
若使用application.yml:
spring: mvc: pathmatch: matching-strategy: ant-path-matcher
内容的提问来源于stack exchange,提问作者drn
相关产品推荐
相关产品推荐

