๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Oct 25 10:15:30 IST 2023
There was an unexpected error (type=Not Found, status=404).
No message availableโก Quick Fix Works 80% of the time
Ensure your controller class is annotated with @RestController and the method matches the exact HTTP path.
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() { return "Hello"; }
}๐ ๏ธ Solutions (3 Ways to Fix)
Verify @RestController annotation
๐ Use this if you wrote a controller method but forgot to annotate the class.
If you use @Controller instead of @RestController, Spring tries to find an HTML view instead of returning JSON, leading to 404s.
@RestController
public class MyController { ... }Check URL path matching
๐ Use this if your annotations are correct but the URL is wrong.
Ensure the URL you are hitting exactly matches the @RequestMapping and @GetMapping paths, including slashes.
// Controller: @GetMapping("/api/users")
// Correct URL: http://localhost:8080/api/users
// Wrong URL: http://localhost:8080/usersFix Component Scan for Controllers
๐ Use this if your controller is in a different package than your main Application class.
Spring Boot only scans the package of the main class and its sub-packages. If your controller is in a sibling package, it won't be registered.
@SpringBootApplication
@ComponentScan(basePackages = {"com.devinhyderabad.controllers", "com.devinhyderabad.services"})๐ Version Notes
Throws NoHandlerFoundException if spring.mvc.throw-exception-if-no-handler-found is true.
Throws NoHandlerFoundException by default without needing the property.
๐ง Why this Happens
Tap to expand the deep technical explanation
Spring Boot's embedded Tomcat server received an HTTP request, but could not find any Java method mapped to that specific URL path. This usually means the controller is missing, the URL is typed wrong, or the controller isn't being scanned by Spring.
The HITEC City Parking Spot Analogy:
It's like dialing a phone number that isn't connected to anyone. The network works, but there's no one at that specific extension to answer.
๐ก๏ธ How to Prevent This Next Time
Always test API endpoints using Postman or curl immediately after creating them to verify the mapping.