简体   繁体   中英

@Controller not working in (module web) child package in Spring boot multi module maven project

@Controller not working in child package in Spring boot multi module maven project?

poms As follows;

api module 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>webDemo</artifactId>
        <groupId>webDemo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>


    <artifactId>api</artifactId>
    <groupId>api</groupId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    </dependencies>

</project>

web module 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>webDemo</artifactId>
        <groupId>webDemo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>
    <groupId>web</groupId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <artifactId>api</artifactId>
            <groupId>api</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.apiDemo.ApiApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Parent 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>
    <packaging>pom</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <modules>
        <module>api</module>
        <module>web</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>
    <groupId>webDemo</groupId>
    <artifactId>webDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
         <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>-->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

When I run the project ApiController.java is working fine by /app url. But when I try to access /web url of WebController.java , I got the error page.

No @Controller or @RestController is working from child (web) module. To solve this issue I added @ComponentScan(basePackages = {"com.web"}) in Application.java , but still I am getting the error page.

Can any help please? Thanks in advance.

I think that the classloader prepared by your IDE is confused by the fact that the "fat jar" overwrites the normal jar. The IDE understands only normal jars.

I always build the jars under different names, so I can avoid this kind of issues.

Change the configuration of spring-boot-maven-plugin , so when a fat jar is compiled, the original is still there: https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html#classifier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM