亲本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>
<groupId>com.sit</groupId>
<artifactId>multi-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>one</module>
<module>app</module>
</modules>
<packaging>pom</packaging>
<name>multi-module</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>应用程序模块POM
spring引导主类驻留在此模块上。
<?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>multi-module</artifactId>
<groupId>com.sit</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app</artifactId>
<dependencies>
</dependencies>
</project>一个模块(子模块) 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>multi-module</artifactId>
<groupId>com.sit</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>one</artifactId>
<packaging>jar</packaging>
<dependencies>
</dependencies>
</project>来自应用程序模块的Spring引导主类
package com.sit.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.sit"})
@EntityScan(basePackages = {"com.sit"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}AppController来自应用程序模块
package com.sit.app.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class AppController {
@GetMapping(value = "/app")
public String getPage(){
return "app";
}
}OneController类来自one模块
package com.sit.one.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class OneController {
@GetMapping(value = "/one")
public String getPage(){
return "one";
}
}

当我运行由"/app“url.But运行的项目时,当我试图访问OneController.java的"/one”url时,我得到了错误page.No @Controller或@RestController从子(1) module.To解决了这个问题--我在Application.java中添加了@ComponentScan(basePackages = {"com.sit"}),但我还是提前得到了错误page.Can --任何帮助please.Thanks。
发布于 2018-09-04 13:46:07
问题是com.sit:one不在com.sit:app的类路径上。因此,在启动应用程序模块时,无法找到one模块的任何类。
解决方案是确保com.sit:one模块是com.sit:app的依赖项,向应用程序模块的pom.xml中添加以下内容
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>one</artifactId>
<version>${project.version}</version>
</dependency>但是,这还不够,因为spring-boot-maven-plugin将为两个模块创建一个fat JAR,而您只需要应用程序模块(可运行模块)的fat JAR。
我建议您将<plugin>移动到应用程序模块的pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>然后,您应该从父pom.xml中删除插件。
https://stackoverflow.com/questions/52167511
复制相似问题