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.
Understanding the Spring Boot 404 Error with REST Controllers
When developing web applications using Spring Boot, REST controllers play a pivotal role in handling HTTP requests. A common issue developers face is encountering a 404 error when trying to access a REST endpoint on localhost. This may occur due to a variety of reasons, ranging from annotation misconfigurations to incorrect URL mappings. This article delves into the technical aspects, common pitfalls, and solutions to resolve this issue.
Typical Setup of a Spring Boot REST Controller
A basic REST controller in Spring Boot might look like this:
When this application is run on a local server, you might expect to reach this endpoint by navigating to http://localhost:8080/hello. However, sometimes you may encounter a 404 error, indicating the server cannot find the requested resource.
Common Reasons and Solutions for 404 Errors
Several factors might contribute to a 404 error when accessing a REST controller in Spring Boot. Below are common reasons and their solutions:
- Incorrect URL Mapping
- Issue: The actual endpoint path does not match the intended path.
- Solution: Ensure that the path specified in the
@GetMappingor@RequestMappingannotation corresponds to the URL you are trying to access. Double-check for typos or missing slashes.
- Port Configuration
- Issue: The application might not be running on the default port.
- Solution: Check the application configuration file (
application.propertiesorapplication.yml) for a custom server port setting:
Access the endpoint via http://localhost:9090/hello if a different port is configured.
- Controller Annotation Issues
- Issue: The class is mistakenly not annotated as a REST controller.
- Solution: Ensure that the class is annotated with
@RestController, not just@Controller.
- Base Path Configuration
- Issue: The application has a base path configured.
- Solution: Spring Boot allows setting a base path for all endpoints:
Access the endpoint via http://localhost:8080/api/hello.
- Component Scan Configuration
- Issue: The package containing the controller is not scanned.
- Solution: Ensure your main application class is in a package above the controller package or specify packages in
@ComponentScan.
- Security Constraints
- Issue: Spring Security or other security constraints might block access.
- Solution: Check security configurations, ensuring that endpoints are properly exposed and accessible.
Example: Diagnosing Through Logging
Enabling debug-level logging can be instrumental in diagnosing the issue:
The logs may provide insights into request mapping details and routing issues.
Example Table: Common Configuration Issues Summary
| Issue | Description | Solution |
| Incorrect mapping | Mismatch between the URL endpoint and the mapping in code. | Verify and correct the mapping annotations. |
| Port mismatch | Server running on a non-default port. | Check and set the correct port in application.properties. |
| Missing annotations | Controller not recognized due to missing @RestController. | Add @RestController annotation. |
| Base path issue | Configured base path not considered. | Adjust URL to include the base path set in configurations. |
| Component scanning | Controller not included due to improper scanning configuration. | Ensure component scanning includes the controller package. |
| Security restrictions | Access blocked by security settings. | Adjust security configurations to allow endpoint access. |
Additional Considerations
- Check Application Logs: Application startup logs can often indicate if a controller is not being recognized or incorrectly registered.
- Test with Curl/Postman: If browser caching or other issues are suspected, use tools like Curl or Postman to isolate the request.
Conclusion
Encountering a 404 error while accessing a REST controller in Spring Boot can be a frustrating experience, but with systematic debugging and attention to potential configuration pitfalls, the issue can usually be resolved efficiently. By understanding the common causes and implementing the appropriate solutions, developers can ensure their REST endpoints are accessible and functional.
Related reading
- Spring Boot Cannot access REST Controller on localhost 404
- Spring Boot can't autowire ConfigurationProperties
- Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup
- Spring Boot ClassNotFoundException when configuring maxUploadSize of CommonMultipartResolver
- Spring boot context load test hangs
- Spring Boot CORS filter - CORS preflight channel did not succeed
- Spring Boot classpath
- Spring Boot Cloud Zuul Proxy 404 Error

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.