簡體   English   中英

在Spring Boot多模塊Maven項目的子包中@Controller,@RestController和@Component不起作用

[英]@Controller , @RestController and @Component not working in child package in Spring boot multi module maven 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">
    <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 Boot主類駐留在此模塊上

 <?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 Boot Main Class

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類

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";
    }
}

這是項目結構

當我運行項目AppController.java時,通過“ / app” URL可以正常工作。但是,當我嘗試訪問OneController.java的“ / one” URL時,出現了錯誤頁面。子代中沒有@Controller或@RestController正在運行(一個)模塊。為解決此問題,我在Application.java中添加了@ComponentScan(basePackages = {“ com.sit”}),但仍然出現錯誤頁面。可以提供任何幫助。謝謝。

問題是com.sit:one不在com.sit:app的類路徑上。 因此,啟動應用程序模塊時,找不到一個模塊的所有類。

解決方案是通過將以下內容添加到應用程序模塊的 pom.xml來確保com.sit:one模塊是com.sit:app的依賴項:

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>one</artifactId>
    <version>${project.version}</version>
</dependency>

但是,這還不夠,因為spring-boot-maven-plugin將創建兩個模塊的胖JAR,而您只需要應用程序模塊 (可運行模塊)的胖JAR。

我建議您將<plugin>移至應用程序模塊pom.xml

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

然后,您應該從父pom.xml刪除該插件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM