You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 17:15:40