SpringBoot 无法从多模块 Java 应用程序中的另一个模块中识别 RestController

我花了很长时间,但我无法解决这个(配置)问题。

技术堆栈 :Java (1.8), Springboot (starter-parent, starter-web), Maven, IntelliJ IDEA

描述:尝试创建一个由2个模块组成的多模块Java应用程序(首先):

  1. 核心模块:主模块(主业务逻辑,其他每个模块都应该通过这个模块进行查看和交互)。此模块包含主应用程序类。
  2. webgateway模块:简单的休息控制器,它将映射请求并调用核心模块

问题:Springboot 在发送 http 请求时未从 webgateway 模块加载/扫描 RestController => 404 错误

Github repohttps://github.com/Sorin-J/Greeter

项目配置:

Greeter 
   |
   + pom.xml (parent pom)
   |
   + -- core                                            
   |     |
   |     + ...
   |     |
   |     + pom.xml
   |
   + -- webgateway 
         |
         + ...
         |
         + pom.xml (depends on core pom.xml)

父项.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bet.jbs</groupId>
    <artifactId>Greeter</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>core</module>
        <module>webgateway</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

核心模块 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>Greeter</artifactId>
        <groupId>com.bet.jbs</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>core</artifactId>

</project>

webgateway module pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>Greeter</artifactId>
        <groupId>com.bet.jbs</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>webgateway</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.bet.jbs</groupId>
            <artifactId>core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

主要应用类从核心模块:

package com.bet.jbs.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.bet.jbs.core", "com.bet.jbs.webgateway"})
@EnableAutoConfiguration
public class MainApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MainApplication.class, args);
    }
}

来自webgateway模块的问候控制器类:

package com.bet.jbs.webgateway.controller;

import com.bet.jbs.core.util.GreetingGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @RequestMapping(value = "/webgreeting", method = RequestMethod.GET)
    public String getGreeting() {
        return "WEBGATEWAY module says " + GreetingGenerator.getRandomGreeting();
    }
}

为了测试一个相同的REST控制器如果位于核心模块中可以正常工作,我在核心模块中也创建了一个类似的GreppingController类(这个工作正常):

package com.bet.jbs.core.controller;

import com.bet.jbs.core.util.GreetingGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/*
 * This REST controller should not be in the CORE component.
 * It is just for proving that this controller is recognized and the other one from WEBGATEWAY component is not.
 *
 */
@RestController
public class GreetingController {

    @RequestMapping(value = "/coregreeting", method = RequestMethod.GET)
    public String getGreeting() {
        return "CORE module says " + GreetingGenerator.getRandomGreeting();
    }
}

答案 1

Spring Boot 主应用程序位于模块中,该模块对模块没有依赖关系。因此,带有控制器的类在运行时将不存在,并且无法在 spring 之前发现。corewebgateway

修复:将依赖项添加到核心或将启动器/主类移动到模块。webgatewaywebgateway

您还可以使用第三个模块来执行启动,并具有 和 的依赖关系。corewebgateway


答案 2

自从上次有一天以来,我就陷入了这个问题...下面的解决方案肯定会为您节省大量时间!

以下是我如何解决它:

  1. 为主类创建单独的模块。Application

    x-服务

    • pom.xml (parent)
    • 子模块1
      • 啪.xml
    • 子模块2
      • 啪.xml
    • 应用模块
      • pom.xml(包含子 1 和子 2 的依赖项)
      • src/main/java/Application.java
  2. 将所有模块作为依赖项添加到具有应用程序类的模块的 pom 中。

     <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                       http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
         <artifactId>x-service</artifactId>
         <groupId>com.a.b.c</groupId>
         <version>0.0.1-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
    
     <artifactId>app-module</artifactId>
    
     <dependencies>
         <dependency>
             <groupId>com.a.b.cs</groupId>
             <artifactId>child1-module</artifactId>
             <version>0.0.1-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>com.a.b.cs</groupId>
             <artifactId>child1-module</artifactId>
             <version>0.0.1-SNAPSHOT</version>
             <scope>compile</scope>
         </dependency>
     </dependencies>
    
     <build>
         <resources>
             <resource>
                 <directory>src/main/resources</directory>
                 <filtering>true</filtering>
             </resource>
         </resources>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
                     <executable>true</executable>
                 </configuration>
             </plugin>
         </plugins>
     </build>
    
  3. 保留主 pom 作为父 pom。

     <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
       http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <packaging>pom</packaging>
     <modules>
         <module>child1-module</module>
         <module>child2-module</module>
         <module>app-module</module>
     </modules>
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.1.2.RELEASE</version>
         <relativePath/>
     </parent>
    
     <groupId>com.a.b.c</groupId>
     <artifactId>x-service</artifactId>
     <version>0.0.1-SNAPSHOT</version>
    
     <properties>
         <java.version>1.8</java.version>
     </properties>
    
     <dependencies>
    
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
    
    
     </dependencies>
    
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-checkstyle-plugin</artifactId>
                 <version>3.0.0</version>
             </plugin>
         </plugins>
     </build>
    
  4. 在所有子 pom 中添加父 pom artifect 作为父项。

     <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
         <artifactId>dxg-service</artifactId>
         <groupId>com.a.b.c</groupId>
         <version>0.0.1-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
    
     <groupId>com.a.b.c</groupId>
     <artifactId>child1-module</artifactId>
     <version>0.0.1-SNAPSHOT</version>
    
     <dependencies>
         <dependency>
             <groupId>com.a.b.c</groupId>
             <artifactId>child2-modulde</artifactId>
             <version>0.0.1-SNAPSHOT</version>
         </dependency>
     </dependencies>
    

更多细节请点击这里: https://medium.com/macoclock/how-create-multi-module-project-in-intellij-on-macos-50f07e52b7f9


推荐