We're starting a Spring Boot app with gradle bootRun which triggers the x.Application class which has the annotation @SpringBootApplication. After starting we can access REST services in the x package but can't access REST services in other packages. How can you configure the classpath accordingly?
-
1Sound like a component scan problem. It's recommended to put the "Application" class in a "root" package, that way the scanning will traverse child packages recursively. Otherwise, you can add a @ComponentScan to scan other packagesPaul Samsotha– Paul Samsotha2016-02-29 07:24:49 +00:00Commented Feb 29, 2016 at 7:24
1 Answer
Assuming your x package is something like com.example.mybootapp and your main Application.class is inside x package then you need to add this
@SpringBootApplication
@ComponentScan({"com.example.mybootapp","com.example.someother","one.more.pack"})
on your main Application.class method or Configuration file.
@SpringBootApplication itself consists of @Configuration @EnableAutoConfiguration @ComponentScan annotations, so @ComponentScan default the basePackge (i.e. packages to scan) to the package of the main Application.class and that is why Spring is unable to detect your other @Controlers which are outside of main package.
If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan without any arguments. All of your application components (@Component, @Service, @Repository, @Controller etc.) will be automatically registered as Spring Beans.
Refer this document on how to structure your code.
1 Comment
@ComponentScan({"com.example.mybootapp","com.example.someother","one.more.pack"}) did the trick.