Maven configuration with Spring Boot & multi modules - run application in Intellij

前端 未结 3 1346
闹比i
闹比i 2020-12-08 16:09

I\'m currently working on a REST API with Spring Boot.

I\'m new to Maven and have just started coding with IDEA (don\'t know well this IDE yet), and I have a problem

相关标签:
3条回答
  • 2020-12-08 16:33

    You need to tell SpringBoot where to look for your controllers. Per default that only happens in sub-packages of your @SpringBootApplication class (which will probably not include your sub module).

    In order to change that you can use @ComponentScan("path.to.package") to change the default package.

    Additionally, you can use @EntityScan to do the same for @Entity classes that might be in your sub-module.

    0 讨论(0)
  • 2020-12-08 16:35

    also note that in case of JPA Entities and repositories are not in sub packages of Application.java's package then @Entityscan and @EnableJpaRepositories MUST be declared in the Application class, e.g:

    @Configuration
    @ComponentScan(basePackages="com.my.pack")
    @EnableAutoConfiguration
    @EnableJpaRepositories(basePackages="com.my.pack")
    @EntityScan(basePackages="com.my.pack")
    public class Application{
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 16:44

    Your project structured as:

    • parent
      • pom.xml
      • main module
        • controller
        • domain
        • App.java (Spring Boot main class)
        • pom.xml (add sub moudle to main module as dependency)
      • sub module
        • controllers
        • domain
        • pom.xm

    if your App.java in a package: com.xxxx.pro,then set the sub module's package is com.xxx.pro,such as your sub module's controller is TestController.java, and the code is:

    package com.xx.pro.web;
    @RestController
    public class TestController{
    
    }
    

    so, this sub moudle's TestController will be sanned by App.java.Try it on, good luck.

    0 讨论(0)
提交回复
热议问题