Mohamed Mamun
Mohamed Mamun

Reputation: 299

String boot multi module application running

For a long time, I am struggling to find a solution. I am trying to build a spring boot multi-module project.

I have two modules for practice purposes, later I will add more modules in my real project.

Module 1 whose name is application is where I have implemented my Controller.

Module 2 (boot) is the runner for the whole project. Both of them are inherited from the parent.

I got stuck, when I start the application, and i make a request to the end-point of the Controller. It seems that, Spring doesn't recognize it.

Project structure:

enter image description here

Controller class

enter image description here

Boot

enter image description here

Request

enter image description here

boot pom.xml

<?xml version="1.0" encoding="UTF-8"?>

4.0.0

<parent>
    <groupId>org.onebox</groupId>
    <artifactId>onebox</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>boot</artifactId>

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

application 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>
    <parent>
        <groupId>org.onebox</groupId>
        <artifactId>onebox</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>application</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

Parent 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>org.onebox</groupId>
    <artifactId>onebox</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>boot</module>
        <module>application</module>
    </modules>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
    </parent>

    <!--<dependencies>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.7.0</version>
        </dependency>
    </dependencies>-->

</project>

Upvotes: -1

Views: 92

Answers (2)

R Penumaka
R Penumaka

Reputation: 171

Refer this on how to structure your code, so that @SpringBootApplication reads your class annotated with @Controller class

https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/html/using-boot-structuring-your-code.html

Upvotes: 0

ILya Cyclone
ILya Cyclone

Reputation: 954

Let's start with your Controller class being in a default package which is

a) a no-no by itself;
b) won't be scanned by your @SpringBootApplication in com.example package.

Upvotes: 0

Related Questions