Search code examples
javamavenintellij-ideaspring-bootmulti-module

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

Solution

  • 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.