gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:41:25 25 4
gpt4 key购买 nike

我已经花了很多时间,但还是无法解决这个(配置)问题。

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

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

  1. core 模块:主要模块(主要业务逻辑,每个其他模块都应该通过这个模块看到和交互)。该模块包含主要应用程序类。
  2. webgateway 模块:简单的 Rest Controller ,它将映射请求并调用 core 模块

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

Github repo :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>
来自 核心模块的

MainApplication类:

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 Controller 位于 core 模块中是否可以正常工作,我也在 中创建了一个类似的 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();
}
}

最佳答案

Spring Boot主应用在core模块中,不依赖于webgateway模块。因此带有 Controller 的类不会在运行时出现,也不会被 spring 发现。

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

您还可以使用第三个模块来启动并依赖于 corewebgateway

关于java - SpringBoot 无法识别多模块 Java 应用程序中另一个模块的 RestController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37133210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com