Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot and controllers in imported modules

I have a Spring boot application and I want to import a dependency written in spring boot that defines some controllers.

Maybe it is straightforward, but how can I made the main application able to initialize all these controllers in the imported module? When I try to access the path to these controllers I get a error for missing handler method for the given path. I tried as follows:

@SpringBootApplication
@ComponentScan(basePackages = {"com.main.project", "com.imported.dependency"})
public class MyApplication
    implements CommandLineRunner {

    public static void main(final String... args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.setWebEnvironment(true);
        app.run(args);

    }
}

i.e. I tried with @ComponentScan, but nothing happens.

I also tried to see if the controllers are loaded:

ApplicationContext ctx = SpringApplication.run(FrontendApplication.class, args);

System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
    System.out.println(beanName);
}

They are not. I tried to remove @SpringBootApplication and to use @EnableAutoConfiguration and @ComponentScan, but this does not work.

Suggestions?

like image 829
mat_boy Avatar asked Oct 20 '15 22:10

mat_boy


People also ask

What are the most two important modules of spring boot?

The Model Module contains Entities and Visual Objects to be used in the project. The Repository module contains repositories to be used in the project. It depends on the Model Module. The Service API module contains all project services.

Can spring boot application have multiple controllers?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.


1 Answers

after the discussion on the main thread I tried to setup a small project that's like yours and I put it on github, I can't see any problem.

Give it a look https://github.com/e-ivaldi/mat_boy_test

This is from the log 2015-10-24 17:22:02.900 INFO 31901 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/**]}" onto public java.lang.String com.somethingelse.controllers.SimpleController.xxx()

like image 114
Emanuele Ivaldi Avatar answered Nov 03 '22 23:11

Emanuele Ivaldi