Issue
The endpoints from the main project are working but I cannot reach endpoints from subproject called backend. Main project has the following structure: apps/main/com/itapp/config. There I have controllers folder, Main.java and ServletInitializer. Backend project has the following structure: backend/src/main/java/com/itapp/backend. There I have controllers folder.
My config:
settings.gradle:
rootProject.name = 'itapp'
include 'backend'
build.gradle
plugins {
id 'java'
id 'application'
id 'org.springframework.boot' version '2.5.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
sourceCompatibility = 1.8
mainClassName = 'com.itapp.config.Main'
sourceSets {
main {
java { srcDirs = ['apps/main'] }
resources { srcDirs = ['apps/main/resources'] }
}
}
dependencies {
implementation project(":backend")
}
bootJar {
baseName = 'itapp'
version = '0.0.1'
}
allprojects {
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.springframework.boot'
group = 'com.itapp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
test {
useJUnitPlatform()
}
}
I have the following project structure:
Root project 'itapp'
\-- Project ':backend'
Solution
Spring scan will look inside all class inside your package looking for @Configuration, @Service, @Component, @Controller....
By default the package is the same as the one from your main application in your case com.itapp.apps
So spring will only find controller in com.itapp.apps package. And because your other controller are in com.itapp.backend
he will not find it.
Adding com.itapp.backend
to the scan will also add your controller to your context and add the endpoint that you want
You can have more information here with a better explaination
Answered By - Ruokki