Spring Boot
REST Controller
localhost
404 error
troubleshooting

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.

Browse interview questions

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:

java
1import org.springframework.web.bind.annotation.GetMapping;
2import org.springframework.web.bind.annotation.RestController;
3
4@RestController
5public class HelloWorldController {
6
7    @GetMapping("/hello")
8    public String sayHello() {
9        return "Hello, World!";
10    }
11}

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:

  1. Incorrect URL Mapping
    • Issue: The actual endpoint path does not match the intended path.
    • Solution: Ensure that the path specified in the @GetMapping or @RequestMapping annotation corresponds to the URL you are trying to access. Double-check for typos or missing slashes.
  2. Port Configuration
    • Issue: The application might not be running on the default port.
    • Solution: Check the application configuration file (application.properties or application.yml) for a custom server port setting:
properties
     server.port=9090

Access the endpoint via http://localhost:9090/hello if a different port is configured.

  1. 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.
  2. Base Path Configuration
    • Issue: The application has a base path configured.
    • Solution: Spring Boot allows setting a base path for all endpoints:
properties
     server.servlet.context-path=/api

Access the endpoint via http://localhost:8080/api/hello.

  1. 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.
  2. 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:

properties
logging.level.org.springframework.web=DEBUG

The logs may provide insights into request mapping details and routing issues.

Example Table: Common Configuration Issues Summary

IssueDescriptionSolution
Incorrect mappingMismatch between the URL endpoint and the mapping in code.Verify and correct the mapping annotations.
Port mismatchServer running on a non-default port.Check and set the correct port in application.properties.
Missing annotationsController not recognized due to missing @RestController.Add @RestController annotation.
Base path issueConfigured base path not considered.Adjust URL to include the base path set in configurations.
Component scanningController not included due to improper scanning configuration.Ensure component scanning includes the controller package.
Security restrictionsAccess 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.