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

Spring Boot+Hibernate请求出现404 Not Found问题求助

Troubleshooting 404 Not Found Error in Your Spring Boot Employee API

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 /api prefix? Missing this is one of the most frequent causes of 404s.
  • Is the spelling of employe exact? Your entity and endpoint use Employe (not Employee), 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, your EmployeController should be in com.example.controller or com.example (not a completely separate package like com.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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:11:05