Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot doesn't recognize RestController from another module in multi module Java application

I've spent quite some time but I can't get past this (configuration) issue.

Technology stack : Java (1.8), Springboot (starter-parent, starter-web), Maven, IntelliJ IDEA

Description: Trying to create a multi module Java application composed (at first) of 2 modules :

  1. core module : main module (main business logic, every other module should see and interact through this one). This module contains the main application class.
  2. webgateway module : Simple Rest Controller that will map a request and make calls to the core module

Issue: Springboot doesn't load/scan the RestController from the webgateway module => 404 error when sending the http request

Github repo : https://github.com/Sorin-J/Greeter

Project configuration :

Greeter 
   |
   + pom.xml (parent pom)
   |
   + -- core                                            
   |     |
   |     + ...
   |     |
   |     + pom.xml
   |
   + -- webgateway 
         |
         + ...
         |
         + pom.xml (depends on core pom.xml)

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>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 module 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 module 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>

MainApplication class from core module :

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

GreetingController class from webgateway module :

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();
    }
}

Just to test that an identical REST controller would work fine if it is located in the core module, I've created a similar GreetingController class also in core module (and this one works fine) :

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();
    }
}
like image 699
Sorin J Avatar asked May 10 '16 08:05

Sorin J


2 Answers

The Spring Boot main application is in the core module, which does not have a dependency on the webgateway module. Therefore the class with the controller will not be present at runtime and can not be discovered by spring.

Fix: Add dependency to the webgateway to the core or move the launcher/main class to the webgateway module.

You can also use a third module that does the launching and has the dependencies to core and webgateway.

like image 199
Thomas Avatar answered Sep 25 '22 13:09

Thomas


I was stuck in this problems since last one day... The solution below will save you a lot of time for sure!

Here is how I solved it:

  1. Create a separate module for main Application class.

    x-service

    • pom.xml (parent)
    • child1-module
      • pom.xml
    • child2-module
      • pom.xml
    • app-module
      • pom.xml (contains dependency for both child1 and child2)
      • src/main/java/Application.java
  2. Add all the modules as dependencies in the pom of the module having application class.

     <?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>
    
  3. Keep the main pom as 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>
     <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>
    
  4. Add the parent pom artifect as parent in all child poms.

     <?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>
    

More details here: https://medium.com/macoclock/how-create-multi-module-project-in-intellij-on-macos-50f07e52b7f9

like image 41
KayV Avatar answered Sep 24 '22 13:09

KayV