Spring boot and controllers in imported modules

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

It may be simple, but how can I get the main application to initialize all these controllers in the imported module? When I try to access the path to these controllers, I get an error for the missing handler method for this path. I tried the following:

@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 check if the controllers are loaded:

 ApplicationContext ctx = SpringApplication.run(FrontendApplication.class, args); System.out.println("Let inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } 

This is not true. I tried to remove @SpringBootApplication and use @EnableAutoConfiguration and @ComponentScan , but this will not work.

Suggestions?

+5
source share
4 answers

after discussing the main thread, I tried to set up a small project similar to yours, and I put it on github, I see no problem.

Take a look at https://github.com/e-ivaldi/mat_boy_test

This is from the journal 2015-10-24 17: 22: 02.900 INFO 31901 --- [main] swsmmaRequestMappingHandlerMapping: mapped "{[/ **]}" to public java.lang.String com.somethingelse.controllers.SimpleController.xxx ( )

+1
source

Perhaps you have a conflict between:

@SpringBootApplication and @ComponentScan.

In Spring, the Bootable Documentation We Can Read

@SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes

Link: @ SpringBootApplication Documentation

Can you remove @SpringBootApplication and replace it with @Configuration and @EnableAutoConfiguration?

0
source

use the @Configuration and @EnableAutoConfiguration annotation.

0
source

@SpringBootApplication automatically scans every class that has a @SpringBootApplication namespace from each jar in the classpath. This annotation is all you need as your project follows Spring's recommended directory structure. See Spring Boot Documentation for structuring your code .

Try the following:

  • Remove this line of code:

     @ComponentScan(basePackages = {"com.main.project", "com.imported.dependency"}) 
  • Move MyApplication so that it is in the root directory. The root directory must follow this naming convention, com.example.project . Thus, the full path for your main Spring boot application class: com.example.project.MyApplication when replacing example and project with the name and project name of your company.

  • Put your controllers in a subpackage (even if they are packed in a separate jar). Therefore, their namespace should be something like this: com.example.project.controllers.

  • Also, be sure to add @Controller or @RestController to your controller classes.

Hope this helps!

0
source

Source: https://habr.com/ru/post/1234157/


All Articles