SpringBoot 无法从多模块 Java 应用程序中的另一个模块中识别 RestController
我花了很长时间,但我无法解决这个(配置)问题。
技术堆栈 :Java (1.8), Springboot (starter-parent, starter-web), Maven, IntelliJ IDEA
描述:尝试创建一个由2个模块组成的多模块Java应用程序(首先):
- 核心模块:主模块(主业务逻辑,其他每个模块都应该通过这个模块进行查看和交互)。此模块包含主应用程序类。
- webgateway模块:简单的休息控制器,它将映射请求并调用核心模块
问题:Springboot 在发送 http 请求时未从 webgateway 模块加载/扫描 RestController => 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)
父项.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();
}
}