Spring Boot Cannot access REST Controller on localhost 404
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot has become immensely popular for developing RESTful services due to its ease of use and quick setup. However, developers often encounter a common issue: the inability to access a REST Controller on localhost, which typically results in a 404 error. This article delves into the reasons behind this problem and provides solutions using detailed technical explanations.
Understanding the 404 Error in a Spring Boot Application
A 404 Not Found error indicates that the server is unable to locate the requested resource. For a Spring Boot RESTful application, this generally means that the mapping of URLs to controller methods has not been established properly.
Common Causes and Solutions
1. Incorrect Request Mapping
Problem:
Often, the @RequestMapping or @GetMapping, @PostMapping, etc., annotations may be incorrect or missing. This usually happens when the specified URL path does not match the one the client is trying to access.
Solution:
Ensure that the path in the annotation matches the URL in the request. Below is an example of a typical Spring Boot REST Controller:
To access the above endpoint, you should use http://localhost:8080/api/greet. The 404 error will occur if you access any other path.
2. Application Not Running or Incorrect Port
Problem:
Your Spring Boot application may not be running on the expected port, or it might not be running at all.
Solution:
- Verify that your application is running. You should see a log indicating: "Tomcat started on port(s): 8080" (or another configured port).
- Check your
application.propertiesorapplication.ymlfile for port configuration:
- Make sure you access the correct port in the URL.
3. Package Scan Issues
Problem:
Spring Boot might not scan the package containing your controller classes. This usually occurs if your @SpringBootApplication annotated class is not in the root package.
Solution:
Ensure that your main application class is correctly annotated and located in the right package, or use @ComponentScan to explicitly specify the packages to scan:
4. Incorrect Method Signature
Problem:
The method signature in your controller does not match the expected parameters or return types.
Solution:
Verify that methods in your REST controller have the correct signature matching those in the client request. Misconfigured methods could look like this:
Additional Debugging Steps
- Server Logs: Always check your Spring Boot server logs for any error messages or warnings that can help diagnose the issue.
- Browser Network Tools: Utilize your browser’s developer tools. The Network tab will display the exact request sent to the server and any errors returned.
- Postman or Curl: Use tools like Postman or
curlto issue requests and verify your endpoints independently of a frontend application.
Table of Common Errors and Solutions
| Issue | Cause | Solution |
404 error for known endpoint | Incorrect path mapping in annotations | Ensure path in annotation matches URL |
| Cannot connect to server | Application not running or wrong port | Start server, check server.port |
| Controller not being detected | Packages not being scanned | Use @ComponentScan or relocate main class |
Method returns 404 on valid request | Method signature mismatch | Correct method signature |
By understanding these potential issues and applying the correct solutions, you can effectively troubleshoot and resolve 404 errors when accessing Spring Boot REST controllers on localhost. This foundational knowledge enables you to build stable and reliable APIs with confidence.
Related reading
- Spring Boot can't autowire ConfigurationProperties
- Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup
- Spring Boot ClassNotFoundException when configuring maxUploadSize of CommonMultipartResolver
- Spring Boot classpath
- Spring boot context load test hangs
- Spring Boot CORS filter - CORS preflight channel did not succeed
- Spring Boot Cloud Zuul Proxy 404 Error
- Spring boot config server

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.