Spring Boot
REST Controller
404 Error
Localhost
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

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:

java
1@RestController
2@RequestMapping("/api")
3public class SampleController {
4
5    @GetMapping("/greet")
6    public String greet() {
7        return "Hello, World!";
8    }
9}

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.properties or application.yml file for port configuration:
properties
  server.port=8080
  • 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:

java
1@SpringBootApplication
2@ComponentScan(basePackages = "com.example.myapp")
3public class MyAppApplication {
4    public static void main(String[] args) {
5        SpringApplication.run(MyAppApplication.class, args);
6    }
7}

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:

java
1@GetMapping("/items")  // Correct signature: List<String> without any parameters
2public List<String> getItems(String category) {
3    // Error prone since it expects a parameter that is not defined
4    return List.of("Item1", "Item2");
5}

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 curl to issue requests and verify your endpoints independently of a frontend application.

Table of Common Errors and Solutions

IssueCauseSolution
404 error for known endpointIncorrect path mapping in annotationsEnsure path in annotation matches URL
Cannot connect to serverApplication not running or wrong portStart server, check server.port
Controller not being detectedPackages not being scannedUse @ComponentScan or relocate main class
Method returns 404 on valid requestMethod signature mismatchCorrect 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
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.