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

IntelliJ构建Spring Boot服务频繁出现404 URI未找到问题求助

解决Spring Boot中@RequestMapping导致的404 URI未找到问题

遇到这种404问题我也碰过好几次,大多是组件扫描或控制器配置的小疏漏,咱们一步步排查:

1. 确认Spring Boot的组件扫描范围

你的Application类在com.company.project,默认情况下@SpringBootApplication会自动扫描这个包及其所有子包,而ProfileController在com.company.project.modules.profiles.controller,理论上应该被扫描到。但如果你手动添加了@ComponentScan,很可能是范围配置错了:

  • 如果你写了@ComponentScan(basePackages = "com.company.project.someother"),那显然会漏掉控制器所在的包;
  • 建议先去掉自定义的@ComponentScan,只用@SpringBootApplication的默认扫描规则试试,很多时候手动配置反而画蛇添足。

另外,启动日志里可以搜一下ProfileController,如果看不到"Initializing bean 'profileController'"这类日志,说明这个类根本没被Spring容器加载,那肯定找不到映射。

2. 检查控制器的核心注解

这是最容易忽略的点:你的ProfileController类上有没有加@RestController或@Controller注解?

  • 只加@RequestMapping是没用的,Spring需要通过@RestController(返回JSON/REST响应)或@Controller(返回视图)来识别这是一个控制器类;
  • 正确的示例代码应该是这样:
package com.company.project.modules.profiles.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController // 必须添加这个注解
@RequestMapping("/profiles")
public class ProfileController {

    @GetMapping("/test")
    public String testEndpoint() {
        return "Profile endpoint works!";
    }
}

3. 验证@RequestMapping的路径是否正确

  • 确认你访问的URI和控制器配置的路径完全匹配:比如类上是@RequestMapping("/profiles"),方法上是@GetMapping("/details"),那完整路径应该是http://localhost:8080/profiles/details,有没有多写/少写斜杠?
  • 注意路径的大小写:虽然Spring Boot默认的ANT_PATH_MATCHER不区分大小写,但如果你配置了spring.mvc.pathmatch.matching-strategy=PATH_PATTERN_PARSER,就会严格区分,这点要留意。

4. 排查编译与IDE相关问题

你说已经清理过IDE缓存,但可以再确认这些点:

  • 检查target/classes(Maven)或build/classes(Gradle)目录里有没有ProfileController.class文件,如果没有,说明代码没编译成功,可能有语法错误或者IDE编译配置问题;
  • 手动执行构建命令:mvn clean compile(Maven)或./gradlew clean build(Gradle),看是否有编译报错,有时候IDE的自动编译会出问题。

5. 分析服务器日志的具体错误

日志提示"错误控制器相关问题",可以重点看这些日志内容:

  • 如果日志里有No mapping found for HTTP request with URI [xxx] in DispatcherServlet,说明确实没有对应的映射注册;
  • 如果有BeanCreationException之类的错误,说明控制器类的实例化出了问题,比如依赖注入失败,这也会导致映射无法注册。

内容的提问来源于stack exchange,提问作者shantanu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:09:50