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

Spring Boot控制器所有API端点返回404问题求助

Troubleshooting Spring API 404 Errors

Hey there! Let's dig into that 404 issue you're hitting with your Spring API. I see your controller code got cut off halfway through the @Autowired line, so I'll start by filling in what's likely the rest of it based on standard Spring practices, then walk through the most common fixes for this problem.

First, here's what your complete controller might look like (adjust if your actual endpoints differ):

package com.example.controllers;

import com.example.models.Movie;
import com.example.services.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;

@RestController
public class MovieController {

    @Autowired
    private MovieService movieService;

    // Example GET endpoint
    @GetMapping("/movies")
    public List<Movie> getAllMovies() {
        return movieService.getAllMovies();
    }

    // Example GET by ID endpoint
    @GetMapping("/movies/{id}")
    public Optional<Movie> getMovieById(@PathVariable Long id) {
        return movieService.getMovieById(id);
    }
}

Now let's go through the most likely reasons your endpoints are returning 404:

  • Check package scanning for your controller
    Spring needs to detect your MovieController to register its endpoints. Make sure your main application class (the one annotated with @SpringBootApplication) is in a parent package of your controller. For example, if your app class lives in com.example, your controller in com.example.controllers will be scanned automatically. If your controller is in a separate package, add @ComponentScan(basePackages = "com.example.controllers") to your main application class to tell Spring where to look.

  • Verify your request mapping paths
    Double-check the paths in your @GetMapping, @PostMapping, etc. If you added a class-level @RequestMapping("/api") to your controller, your full endpoint would be /api/movies instead of just /movies. Also, watch out for typos in the URL you're testing (while Spring's path matching is case-insensitive by default, it's easy to mistype a segment like /movie instead of /movies).

  • Confirm you're using the correct port
    By default, Spring Boot runs on port 8080. If you changed the port in application.properties (with server.port=xxxx) or application.yml, make sure you're sending requests to that port (e.g., http://localhost:8081/movies instead of http://localhost:8080/movies).

  • Check for a server context path
    If you configured a context path (like server.servlet.context-path=/myapp in your properties file), all your endpoints will be prefixed with that path. So your /movies endpoint becomes http://localhost:8080/myapp/movies.

  • Ensure your service is properly annotated
    Make sure your MovieService class is annotated with @Service—this tells Spring to create a bean for it, which is required for @Autowired to work. While a missing @Service usually throws a startup error, it's worth ruling out if your app is starting without errors but still returning 404s.

  • Use Spring Boot Actuator to inspect mappings (optional but powerful)
    If you add the Actuator dependency to your project (include spring-boot-starter-actuator in your pom.xml or build.gradle), you can hit the /actuator/mappings endpoint to see every registered path in your app. This will show you exactly what Spring has mapped, which is a great way to confirm if your controller's endpoints are being picked up at all.

If you can share the rest of your controller code and your main application class, I can help narrow down the issue even further!

内容的提问来源于stack exchange,提问作者relentless-coder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:42:31