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

Spring官方REST示例运行返回404 Not Found错误求助

404错误排查:Spring REST服务访问/greeting路径失败

问题描述

按照Spring官方REST服务示例步骤操作,使用Postman发送GET请求访问/greeting路径时返回404错误:

"timestamp": "2023-01-04T09:23:03.965+00:00",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/greeting"

项目代码文件如下:

Greeting.java

package com.example.restservice;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

GreetingController.java

package com.example.restservice;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.1'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.session:spring-session-core'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

排查原因

  1. 缺失Spring Boot启动类:Spring Boot需要一个标注@SpringBootApplication的启动类来扫描包内的组件(如@RestController标记的控制器)。你的项目中没有这个类,导致Spring无法发现GreetingController,无法注册/greeting路由。
  2. 依赖冲突:同时引入了spring-boot-starter-web(Servlet栈)和spring-boot-starter-webflux(Reactor栈),两种Web栈的自动配置会相互干扰,导致路由无法正常初始化。

解决方法

1. 添加Spring Boot启动类

在com.example.restservice包下创建RestServiceApplication.java:

package com.example.restservice;

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

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

@SpringBootApplication包含@ComponentScan注解,会自动扫描当前包及子包下的所有Spring组件,让Spring发现并注册GreetingController的路由。

2. 清理冲突依赖

修改build.gradle的依赖块,只保留官方示例需要的依赖,移除冲突和不必要的依赖:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

删除spring-boot-starter-data-rest、spring-boot-starter-web-services、spring-boot-starter-webflux、spring-session-core和io.projectreactor:reactor-test,避免自动配置冲突。

3. 重启应用测试

重新构建项目并启动Spring Boot应用,再次用Postman发送GET请求到http://localhost:8080/greeting,应返回正常响应:

{"id":1,"content":"Hello, World!"}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 18:10:37