0
0
Spring Bootframework~30 mins

Multi-module project structure in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Multi-module project structure
📖 Scenario: You are building a Spring Boot application that has two modules: core for business logic and web for the web interface. This helps keep your code organized like departments in a company.
🎯 Goal: Create a multi-module Spring Boot project with a core module containing a service class and a web module that depends on core and exposes a REST endpoint.
📋 What You'll Learn
Create a parent pom.xml with modules core and web
In core module, create a service class GreetingService with a method getGreeting() returning "Hello from Core!"
In web module, create a Spring Boot application class WebApplication
In web module, create a REST controller GreetingController that uses GreetingService to return the greeting at /greet
💡 Why This Matters
🌍 Real World
Large Spring Boot applications often split code into modules to keep code organized and reusable, like departments in a company.
💼 Career
Understanding multi-module projects is important for working on enterprise Java applications and collaborating in teams.
Progress0 / 4 steps
1
Create parent pom.xml with modules
Create a parent pom.xml file that defines packaging as pom and includes two modules: core and web. Set the groupId to com.example and version to 1.0.0.
Spring Boot
Need a hint?

The parent pom.xml should have packaging set to pom and list core and web as modules.

2
Create core module with GreetingService
In the core module, create a Spring Boot pom.xml with packaging jar and parent set to the root project. Then create a class GreetingService in package com.example.core with a method getGreeting() that returns the string "Hello from Core!".
Spring Boot
Need a hint?

Make sure the core module pom.xml sets the parent and packaging correctly. The GreetingService class should have a method returning the exact string.

3
Create web module with Spring Boot application
In the web module, create a pom.xml with packaging jar, parent set to the root project, and a dependency on the core module. Then create a Spring Boot application class WebApplication in package com.example.web with the @SpringBootApplication annotation and a main method.
Spring Boot
Need a hint?

Remember to add the core module as a dependency in the web module pom.xml. The WebApplication class needs the @SpringBootApplication annotation and a main method to start.

4
Create GreetingController using GreetingService
In the web module, create a REST controller class GreetingController in package com.example.web. Inject GreetingService from the core module using constructor injection. Add a @GetMapping for /greet that returns the greeting string from GreetingService.getGreeting().
Spring Boot
Need a hint?

Use constructor injection to get GreetingService into GreetingController. Annotate the class with @RestController and add a method with @GetMapping("/greet") that returns the greeting.