REST Controller Not found in Spring Boot applications: how to solve it

A pretty common issue for Spring Boot application is a 404 Not Found Error for your REST Controllers. Let’s learn how to fix it in one minute!

So, you have coded your Controller, but when requesting it you are unable to find it:

package com.example.controller; 
@RestController // shorthand for @Controller and @ResponseBody rolled together
public class ExampleController {
   @RequestMapping( "/hello" )
   public String echo() {
      return "Hello World!";


And here is your Application Main class:

package com.example.application;
public class MainApp {
   public static void main( String[] args ) { MainApp.class, args );

To your suprise, you will find that when requesting the “/hello” endpoint a 404 error will be returned:

  "timestamp": 9853742596220,
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/hello"

To understand what is the issue, we need to focus on the @SpringBootApplication annotation. As a matter of fact, this annotation it’s a combination of three annotations:


The default @ComponentScan annotation (without arguments) tells Spring to scan the current package and all of its sub-packages..

Therefore, only classes under the package “com.example.application” will be scanned.

There are two ways to fix it.

The recommended option, is to place the Main Application class at the root of your project, so it will automatically found the Controller (and other classes) which are placed in sub-packages. For example, here is a package structure that works:

└── main
    ├── java
    │   └── com
    │       └── myapp
    │           └── demo
    │               ├── controllers
    │               │   └──
    │               ├── entities
    │               │   └──
    │               ├──
    │               └── repositories
    │                   └──
    └── resources

In the above project, the DemoApplication main class is located at the root of other packages, therefore you will not have issues in finding other classes.

As another option, you could add to your @ComponentScan annotation the starting point used to scan for Classes:

@ComponentScan(basePackages = "com.myapp.demo")
public classDemoApplication {
   // ...

We have just covered some options to fix a common error in Spring Boot applications, related to REST Controller not found.

Exit mobile version