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:
First, make sure Spring recognizes your UsuarioController as a REST endpoint handler:
- Ensure the class is annotated with
@RestController(this combines@Controllerand@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 hittinghttp://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
@Controllerinstead of@RestController, make sure each endpoint method has@ResponseBodyto return JSON/XML instead of a view.
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 incom.myapp.controlleror a deeper subpackage. - If your components are in unrelated packages, add
@ComponentScanto 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); } }
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:treein your terminal to check for dependency conflicts (e.g., multiple versions of Spring libraries). Resolve any conflicts by excluding unwanted dependencies.
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
DispatcherServletinitialized successfully (look forDispatcherServlet initialized successfully)—this is the core servlet that handles all incoming requests.
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).
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
Usuariohas@Entityannotation, and your primary key field has@Id(and@GeneratedValueif 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

