@RestController in other package doesn't work @RestController in other package doesn't work spring spring

@RestController in other package doesn't work


Use basePackages:

@ComponentScan(basePackages = { "com.person","com.controller"} )


I had the same problem the answers provided here worked for me but i had to add another spring annotation and it's more general in case dealing with a lot of repositories.We have the following structure :

 |-src/main/java    |--com.person        |--repositories       |--controllers       |--...

This then should be added in th main

@SpringBootApplication(scanBasePackages = {"com.person"}) @EnableMongoRepositories(basePackages = "com.person.repositories")public class MainDemoApplication { //}


Using a @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan.

From the documentation:

ComponentScan configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's element.

One of basePackageClasses(), basePackages() or its alias value() may be specified to define specific packages to scan. If specific packages are not defined scanning will occur from the package of the class with this annotation.

You can either move it as you did or specify basePackages in @ComponentScan.