springboot无法从另一个模块识别restcontroller

mlnl4t2r  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(465)

我花了相当长的时间,但我无法克服这个(配置)问题。
技术栈:java(1.8)、springboot(starter父级、starter web)、maven、intellij idea
描述:尝试创建一个由两个模块组成的多模块java应用程序(首先):
核心模块:主模块(主业务逻辑,其他每个模块都应该通过这个模块看到和交互)。此模块包含主应用程序类。
webgateway模块:简单的rest控制器,它将Map请求并调用核心模块
问题:springboot没有从webgateway模块加载/扫描restcontroller=>404发送http请求时出错
github回购:https://github.com/sorin-j/greeter
项目配置:

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

父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">

    <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模块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模块中的greetingcontroller类:

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控制器如果位于核心模块中,它是否可以正常工作,我在核心模块中创建了一个类似的greetingcontroller类(这个类可以正常工作):

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();
    }
}
iih3973s

iih3973s1#

spring-boot主应用程序位于 core 模块,它不依赖于 webgateway 模块。因此,带有控制器的类在运行时将不存在,并且不能被spring发现。
修复:将依赖项添加到 webgateway 或者将启动器/主类移到 webgateway 模块。
您还可以使用第三个模块进行启动,并具有要启动的依赖项 core 以及 webgateway .

suzh9iv8

suzh9iv82#

从上一天起我就陷入了这样的困境。。。下面的解决方案肯定会为您节省很多时间!
我是这样解决的:
为main创建一个单独的模块 Application 班级。
x服务
pom.xml(父级)
儿童1模块
pom.xml文件
儿童2模块
pom.xml文件
应用程序模块
pom.xml(包含child1和child2的依赖关系)
src/main/java/application.java
将所有模块作为依赖项添加到具有应用程序类的模块的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>

保持主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>

将父pom工件添加为所有子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">
 <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>

相关问题