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

Spring Boot REST接口Windows下404报错求助(Mac运行正常)

Spring Boot接口404问题排查(Windows环境)

我的Spring Boot REST应用启动正常无报错,但访问http://localhost:8080/hello返回404白标页面。项目在MacBook上运行正常,但在Windows系统中,无论是用Eclipse、STS还是切换Java浏览视角,问题都没解决。

核心代码

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

UserController.java

package com.example.demo;

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

@RestController
public class UserController {
    
    @GetMapping("/hello")
    public String hello() {
        return "Hello Good Morning";
    }

}

pom.xml配置

<?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.1.0-M1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

排查与解决步骤

  • 检查包扫描与目录结构:@SpringBootApplication默认扫描启动类所在包及其子包,虽然代码里启动类和控制器都在com.example.demo下,但Windows文件系统不区分大小写,可能实际目录名和代码里的包名大小写不一致(比如Demo和demo)。手动检查项目的目录结构,确保UserController所在目录完全匹配包名。如果还是不行,直接在@SpringBootApplication里指定扫描包:@SpringBootApplication(scanBasePackages = "com.example.demo")。
  • 清理缓存重新构建:Windows下IDE的编译缓存容易出问题,删掉项目的target目录,然后执行Maven的clean和install命令,再重启应用。用Eclipse/STS的话,右键项目选Maven -> Update Project,勾选强制更新快照/发布版,重新编译运行。
  • 查看启动日志:启动时盯着控制台日志,找有没有类似Mapping GET /hello to public java.lang.String com.example.demo.UserController.hello()的记录,没有的话说明控制器没被加载,就是包扫描的问题。
  • 验证Java版本:Spring Boot 3.1.0-M1要求Java 17+,确认Windows上装的JDK是17版本,IDE里的项目JRE配置也是17。命令行输java -version看版本,IDE里右键项目 -> Build Path -> Configure Build Path,检查JRE System Library。
  • 检查端口与运行配置:确认启动日志里Tomcat用的是8080端口,或者改个端口试试(新建application.properties加server.port=8081,访问http://localhost:8081/hello)。另外检查IDE的运行配置,确保Main class选的是com.example.demo.DemoApplication,不行就删掉旧配置,重新右键启动类选Run As -> Spring Boot App。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 17:54:57