Solution :
When we are creating a Spring boot application we must annotate it with @SpringBootApplication annotation. This annotation will 'wrap up' many other necessary annotations for the application to work smoothly without errors. One such annotation as @ComponentScan annotation .Use of this annotation is to tell the Spring to look for all required Spring components and configure the application to run smoothly.
Application class must be present at the top of your package hierarchy, to allow Spring to scan sub-packages and look for the other required components.
Please find the sample code for your reference as follows:
package com.test.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Following code snippet works like the controller package is under com.test.spring.boot package
package com.test.spring.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyHomeController {
@RequestMapping("/")
public String myhome(){
return "Hello World!";
}
}
Following code snippet will NOT Work as the controller package as it is NOT under com.test.spring.boot package
package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyHomeController {
@RequestMapping("/")
public String myhome(){
return "Hello World!";
}
}
If you follow above given approach your issue will be resolved.