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

Spring Boot Controller映射失败(Whitelabel Error Page 404)求助

Spring Boot 3.0.3 404 Whitelabel错误排查问题

在IntelliJ和STS 4中运行Spring Boot 3.0.3项目时,均出现Whitelabel Error Page 404错误,仅将@RestController放在主应用类内部时可正常运行,因此推测是包结构问题。

已尝试以下方案但均无效:

  • 添加/不添加@ComponentScan注解
  • 将Controller放在主类同一包或子包下
  • 使用@Controller配合/不配合@ResponseBody
  • 仅使用@RequestMapping或@GetMapping定义请求路径
  • 配置不同的@ComponentScan扫描路径
  • 更换浏览器测试

访问地址为:http://localhost:8080/hello

项目代码

主类IfredApplication.java(包路径:com.ifred.app)

package com.ifred.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"com.ifred.app"})

public class IfredApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(IfredApplication.class, args);
    }

}

Controller类hello.java(包路径:com.ifred.app.controller)

package com.ifred.app.controller;

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

@RestController
@RequestMapping("/hello")
public class hello {
    
    @GetMapping
    public String hi() {
        return"Hello world";
    }

}

POM文件内容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ifred</groupId>
    <artifactId>ifred</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ifred</name>
    <description>projeto ifood</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制台输出

[2m2023-02-26T10:43:59.509-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.ifred.app.IfredApplication          [0;39m [2m:[0;39m Starting IfredApplication using Java 17.0.6 with PID 5492 (D:\Frederico\Softwares\Projetos Spring Tools Suite\ifred\target\classes started by Pichau in D:\Frederico\Softwares\Projetos Spring Tools Suite\ifred)
[2m2023-02-26T10:43:59.513-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.ifred.app.IfredApplication          [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2023-02-26T10:44:00.280-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2023-02-26T10:44:00.290-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2023-02-26T10:44:00.291-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.apache.catalina.core.StandardEngine   [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.5]
[2m2023-02-26T10:44:00.391-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2023-02-26T10:44:00.391-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 830 ms
[2m2023-02-26T10:44:00.710-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2023-02-26T10:44:00.721-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[           main][0;39m [36mcom.ifred.app.IfredApplication          [0;39m [2m:[0;39m Started IfredApplication in 1.597 seconds (process running for 2.4)
[2m2023-02-26T10:44:09.601-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2023-02-26T10:44:09.602-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2023-02-26T10:44:09.602-03:00[0;39m [32m INFO[0;39m [35m5492[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 0 ms

补充信息

问题似乎出在首次启动项目时的结构上:

  • 将项目文件发给朋友,他的电脑无法运行;
  • 朋友用Spring Initializr按相同方式创建项目并首次运行后,该项目在所有电脑上都能正常运行;
  • 朋友发给我的项目文件,在我的电脑上首次运行也无法正常工作。

内容的提问来源于stack exchange,提问作者f.e.7777

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 05:37:58