I'm trying to do a getmapping method using rest-api which will return as a JSON on localhost: 8080. When I do this from the default Application class it works great but when I move this functionality to another class nothing happens. Could someone help me with this, please?


The issue is that your package structure prevents
BookControllerfrom being component scanned.When you have your
@SpringBootApplicationdefined in packagecom.xenon.myspringbootapp, anything in that package and in nested packages is eligible for component scanning, which will register a bean out of@(Rest)Controllerand other stereotypes.Because
BookControlleris defined inbook, it is outside of the component scanned packages and will therefore not be component scanned by your@SpringBootApplicationannotation.See the reference docs for more best practices on package structure with Spring Boot applications.
To resolve this, there are two choices to get your class component scanned.
The first (and the way I would recommend) is just to restructure your packages so that the
@SpringBootApplicationclass is at the "base" of your application packages. For example, you could movebook.BookControllerto be atcom.xenon.myspringbootapp.book.BookController.The second is to change the component scanning configuration to include your
bookpackage. You can do this either on the@SpringBootApplicationannotation itself:Or, define a different
@ComponentScan. Note that the configuration class annotated with@ComponentScanmust still be component scanned itself.