SpringBoot가 다중 모듈 Java 애플리케이션의 다른 모듈에서 RestController를 인식하지 못합니다.
시간을 꽤 들였지만 이 (구성) 문제를 극복할 수 없습니다.
기술 스택: Java (1.8), Springboot (스타트-부모, 스타터-웹), 메이븐, IntelliJ IDEA
설명:(처음에는) 두 개의 모듈로 구성된 다중 모듈 Java 응용 프로그램을 만들려고 합니다.
- 핵심 모듈: 메인 모듈(메인 비즈니스 로직, 다른 모든 모듈은 이 모듈을 통해 보고 상호 작용해야 합니다.이 모듈에는 기본 응용 프로그램 클래스가 포함되어 있습니다.
- 웹게이트웨이 모듈 : 코어 모듈에 요청을 매핑하고 호출하는 Simple Rest Controller
문제: Springboot가 http 요청을 보낼 때 웹 게이트웨이 모듈에서 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)
상위 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>
core 모듈 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);
}
}
웹 게이트웨이 모듈에서 컨트롤러 클래스 인사:
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 컨트롤러가 코어 모듈에 있으면 정상적으로 작동하는지 테스트하기 위해 코어 모듈에도 유사한 그리팅 컨트롤러 클래스를 만들었습니다(이 클래스는 정상적으로 작동합니다).
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에 .coremodule.에입니다.webgateway모듈. 는 런타임에 않으며 할 수 .따라서 컨트롤러가 포함된 클래스는 런타임에 존재하지 않으며 봄까지 검색할 수 없습니다.
수정: 에 종속성 추가webgateway를 으메이런로나/인클를래로 합니다.webgateway모듈.
또한 시작을 수행하고 다음과 같은 종속성을 가진 세 번째 모듈을 사용할 수 있습니다.core그리고.webgateway.
나는 지난 어느 날부터 이 문제에 갇혀 있었습니다.아래의 솔루션은 확실히 많은 시간을 절약해 줄 것입니다!
문제를 해결한 방법은 다음과 같습니다.
메인 모듈을 별도로 생성합니다.
Application학생들x-service
- pom.xml(부모)
- 소아 1세부터 소아 1세까지의
- pom.xml
- 소아 2세의
- pom.xml
- 앱에 표시된
- pom.xml(child1 및 child2 모두에 대한 종속성 포함)
- src/main/java/Application.자바
응용 프로그램 클래스가 있는 모듈의 폼에 모든 모듈을 종속성으로 추가합니다.
<?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>메인 폼을 부모 폼으로 유지합니다.
<?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>모든 자식 폼에서 부모 폼 아티팩트를 부모로 추가합니다.
<?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
올바른 종속성을 추가했지만 Spring Boot은 여전히 새 컨트롤러를 찾지 못했습니다.모듈이 추가되면 IntelliJ IDEA(2020.3.2) 또는 Maven(3.3.6)이 제대로 업데이트되지 않는 것 같습니다.이것으로 해결되었습니다.
메이븐: 청소 + 상위 프로젝트에 설치
파일 메뉴의 IntelliJ에서
Invalidate Caches / Restart..
앞서 언급했듯이, 당신은 스프링 웹 스타터를 설정한 부모 폼을 가지고 있어야 하고, 다른 모듈들은 그 부모 폼을 그들의 부모로 가져야 합니다. 당신의 루트 폼 프로젝트를 메이븐에 추가하기 위해 조심하세요. IDE에서 이 오픈 메이븐을 하고 폼을 추가하기 위해서.
언급URL : https://stackoverflow.com/questions/37133210/springboot-doesnt-recognize-restcontroller-from-another-module-in-multi-module
'programing' 카테고리의 다른 글
| Github: 업스트림 분기를 포크로 가져옵니다. (0) | 2023.07.16 |
|---|---|
| C에서 스크린 메모리에 쓸 수 없습니다. (0) | 2023.07.16 |
| C 및 C++ 함수 서명에 사용되는 휴대용 USED 매개 변수 매크로 (0) | 2023.07.16 |
| SQL Server에서 커서를 사용하는 것이 잘못된 관행으로 간주되는 이유는 무엇입니까? (0) | 2023.07.16 |
| 핵심 데이터를 사용하여 열거형을 구현하는 가장 좋은 방법 (0) | 2023.07.16 |