You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot REST API 404错误:JDK1.8基础REST应用创建遇问题

Troubleshooting Spring Boot REST API 404 Error (JDK 1.8)

Hey there, let’s work through this 404 issue step by step—since you’re using JDK 1.8 with Spring Boot, here are the most common culprits and fixes:

1. Incomplete or Redundant Startup Class

First, your provided DemoApplication code cuts off at publi...—a valid Spring Boot startup class must have a main method to bootstrap the application. Also, note that @SpringBootApplication already includes @ComponentScan and @EnableAutoConfiguration, so adding those explicitly is redundant (and can cause confusion if misconfigured). Here’s the corrected, complete startup class:

package org.demo;

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

// @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication
public class DemoApplication {

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

2. Missing or Misconfigured REST Controller

The #1 cause of 404s in Spring Boot REST apps is no valid controller being detected. You haven’t shared your controller code, so let’s confirm you have one set up correctly:

  • Create a class annotated with @RestController (this combines @Controller and @ResponseBody for REST responses)
  • Ensure the controller is in the org.demo package (or a subpackage like org.demo.controller)—this is critical because @SpringBootApplication scans the startup class’s package and all subpackages by default.

Example valid controller:

package org.demo.controller;

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

@RestController
public class DemoController {

    // Define a GET endpoint at /hello
    @GetMapping("/hello")
    public String getHelloMessage() {
        return "Hello from your Spring Boot REST API!";
    }
}

3. Incorrect Dependencies

Since you’re using JDK 1.8, you must use Spring Boot 2.x (Spring Boot 3.x requires JDK 17+). Make sure your build file includes the spring-boot-starter-web dependency, which provides all the web/REST functionality:

Maven (pom.xml)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.17</version> <!-- Last Spring Boot 2.x version supporting JDK 8 -->
    <relativePath/>
</parent>

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

Gradle (build.gradle)

plugins {
    id 'org.springframework.boot' version '2.7.17'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id 'java'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

4. Wrong Request Path/Port

Double-check your request details:

  • If your controller uses @GetMapping("/api/hello"), your request URL must be http://localhost:8080/api/hello (not just /hello)
  • If you’ve modified the default port in application.properties (e.g., server.port=8081), use that port instead of 8080
  • Ensure there are no typos in the endpoint path (case-sensitive in most setups)

5. Component Scan Scope Issues

If your controller is in a package outside org.demo, you’ll need to explicitly include it in @ComponentScan. For example:

@SpringBootApplication(scanBasePackages = {"org.demo", "com.other.controller"})
public class DemoApplication { ... }

Quick Test

Once you’ve fixed the above, start your app and send a GET request to http://localhost:8080/hello (or your endpoint path). If you get the expected response, the 404 should be resolved!

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

火山引擎 最新活动