Spring Boot+Hibernate请求出现404 Not Found问题求助
Hey there! Let's break down why you're hitting that 404 error when testing your API with Postman. Based on the code you shared, here are the most common fixes to check step by step:
1. Verify Your Request Path is Correct
Your controller uses @RequestMapping("/api") at the class level, and the GET endpoint is mapped to /employe. That means your full request URL should be:
http://localhost:8080/api/employe
Double-check these details in Postman:
- Did you include the
/apiprefix? Missing this is one of the most frequent causes of 404s. - Is the spelling of
employeexact? Your entity and endpoint useEmploye(notEmployee), so avoid typos here. - If you modified the default port in
application.properties(e.g.,server.port=8081), make sure you're using that port instead of 8080.
2. Ensure Component Scanning is Working
Spring Boot automatically scans for components (Controllers, Services, Repositories) that live in the same package or sub-packages as your main application class. For example:
- If your main class is
com.example.MySpringBootApp, yourEmployeControllershould be incom.example.controllerorcom.example(not a completely separate package likecom.other.controller).
If your classes are outside the default scan range, add @ComponentScan to your main application class to specify the packages to scan:
@SpringBootApplication @ComponentScan(basePackages = {"your.package.controller", "your.package.service", "your.package.dao"}) public class YourMainApplicationClass { public static void main(String[] args) { SpringApplication.run(YourMainApplicationClass.class, args); } }
3. Check for Correct Annotations & Imports
Make sure your EmployeController uses the right @RestController annotation from Spring's web package. The correct import is:
import org.springframework.web.bind.annotation.RestController;
Using an incorrect import (from a non-Spring library) will prevent Spring from recognizing this class as a valid controller.
4. Check Application Startup Logs
When you launch your Spring Boot app, look for a log line like this in the console:
Mapped "{[/api/employe],methods=[GET]}"
This line confirms that Spring has successfully registered your endpoint. If you don't see this line, your controller isn't being detected—head back to step 2 to fix component scanning.
If there are any errors in the startup logs (e.g., database connection issues, failed bean injection), resolve those first. A failed startup can leave your API endpoints unavailable even if the app appears to run.
5. Optional: Use Actuator to Verify Endpoints
If you add the Spring Boot Actuator dependency, you can easily view all registered endpoints:
<!-- For Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Then add this to application.properties to expose the mappings endpoint:
management.endpoints.web.exposure.include=mappings
Visit http://localhost:8080/actuator/mappings in your browser or Postman to confirm if your /api/employe endpoint is listed.
内容的提问来源于stack exchange,提问作者Akash

