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

Spring Boot+JPA连接AWS MySQL正常,但RequestMapping请求404求助

Hey Hugo, let's work through why all your endpoints are returning 404 even though your app connects to the AWS MySQL database successfully. Here are the most common culprits and fixes to check step by step:

1. Validate Your Controller Annotations

First, make sure Spring recognizes your UsuarioController as a REST endpoint handler:

  • Ensure the class is annotated with @RestController (this combines @Controller and @ResponseBody, so you don't need both).
  • Double-check the request mapping annotations (@RequestMapping, @GetMapping, etc.) match the URL you're calling. For example, if you're hitting http://localhost:8080/api/usuarios, your controller should look something like this:
    @RestController
    @RequestMapping("/api/usuarios") // Base path for all endpoints in this controller
    public class UsuarioController {
        private final UsuarioService usuarioService;
    
        // Constructor injection (preferred over @Autowired)
        public UsuarioController(UsuarioService usuarioService) {
            this.usuarioService = usuarioService;
        }
    
        @GetMapping("/") // Maps to GET /api/usuarios/
        public List<Usuario> getAllUsuarios() {
            return usuarioService.getAllUsuarios();
        }
    }
    
  • If you're using @Controller instead of @RestController, make sure each endpoint method has @ResponseBody to return JSON/XML instead of a view.
2. Check Component Scanning & Main Class Location

Spring Boot automatically scans components (controllers, services, repositories) in the main class's package and its subpackages. If your UsuarioController or UsuarioService is outside this scope, Spring won't register them:

  • For example, if your main class is in com.myapp, your controller should be in com.myapp.controller or a deeper subpackage.
  • If your components are in unrelated packages, add @ComponentScan to your main class to explicitly define the scan paths:
    @SpringBootApplication
    @ComponentScan(basePackages = {"com.myapp.controller", "com.myapp.service", "com.myapp.repository"})
    public class YourApplication {
        public static void main(String[] args) {
            SpringApplication.run(YourApplication.class, args);
        }
    }
    
3. Confirm Maven Dependencies Are Correct

Missing or conflicting dependencies can prevent Spring from setting up the web layer properly:

  • Make sure you have the Spring Web Starter in your pom.xml—this is required for handling HTTP requests:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  • Run mvn dependency:tree in your terminal to check for dependency conflicts (e.g., multiple versions of Spring libraries). Resolve any conflicts by excluding unwanted dependencies.
4. Dig Into Application Startup Logs

Your startup logs will tell you if Spring is actually registering your endpoints:

  • Look for lines like Mapped "{[/api/usuarios/],methods=[GET]}"—this confirms the controller endpoint is mapped correctly. If you don't see these lines, your controller isn't being picked up.
  • Check for warnings or errors about component scanning (e.g., "No qualifying bean of type UsuarioService found")—this would mean your service isn't registered, which would cause issues even if the endpoint was mapped.
  • Verify the DispatcherServlet initialized successfully (look for DispatcherServlet initialized successfully)—this is the core servlet that handles all incoming requests.
5. Double-Check Your Request URL & HTTP Method

Small typos here are easy to miss:

  • Ensure the URL matches exactly (including slashes and case—Spring Boot is case-insensitive by default, but it's best to match your annotations). For example, if your controller uses /api/usuarios, don't call /api/usuario.
  • Confirm you're using the correct HTTP method: if your endpoint uses @GetMapping, a POST request will return a 404 (or 405 Method Not Allowed, depending on configuration).
6. Rule Out JPA/Entity Issues (Indirect Check)

While database connectivity works, a misconfigured Usuario entity won't cause 404s, but it could break your service layer (leading to 500 errors instead). Just to be safe:

  • Ensure Usuario has @Entity annotation, and your primary key field has @Id (and @GeneratedValue if using auto-increment).
  • Confirm your repository interface extends JpaRepository<Usuario, YourIdType>.

If you've checked all these and still have issues, share snippets of your UsuarioController code, relevant startup log lines, and your pom.xml dependencies—this will help narrow down the problem faster!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:46:26