Maven Monorepo
This project is a Maven-based monorepo containing multiple modules for a Spring Boot application. The project structure is organized into modules for microservices and shared libraries, facilitating maintenance and scalable development.
Module Structure
The project structure is organized as follows:
- monorepo: The root module of the project that defines the global configuration and main modules.
- microservices: A module containing the microservices:
- usermanagement: User management.
- feedbackrequest: Feedback request.
- feedbackresponse: Feedback response.
- libs: A module containing shared libraries:
- security: Implementations related to security.
Project Configuration (POM)
monorepo/pom.xml
This is the main pom.xml file that defines the project as a parent module for other modules.
microservices/pom.xml
Defines the microservices module and its dependencies, including Spring Boot, JPA, and other necessary libraries.
libs/pom.xml
Defines the libs module, which contains shared library modules like security.
Usage Examples
Creating an Endpoint in Spring Boot
To create an endpoint, you can add a controller to the desired microservice module. Example:
package com.ciandt.nextgen25.usermanagement.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/users")
public String getUsers() {
return "List of users";
}
}
Accessing the Database
The project is configured to use JPA. To access the database, create an entity and a repository. Example:
package com.ciandt.nextgen25.usermanagement.model;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
// Getters and Setters
}
package com.ciandt.nextgen25.usermanagement.repository;
import com.ciandt.nextgen25.usermanagement.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
Creating Tests with TDD
To create tests, use the JUnit framework. Example of a simple test:
package com.ciandt.nextgen25.usermanagement;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser(username = "admin", roles = {"ADMIN"})
public void testCreateUser() throws Exception {
mockMvc.perform(
post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"id\": 1, \"name\": \"John Doe\"}")
)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name").value("John Doe"));
}
@Test
public void testGetUserById() throws Exception {
mockMvc.perform(get("/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("John Doe"));
}
}
Glossary
- Maven: A build automation and dependency management tool for Java projects.
- POM (Project Object Model): An XML file that contains information about the project and configuration details for Maven.
- Spring Boot: A Java framework for creating stand-alone, production-grade applications.
- Microservices: An architecture that structures an application as a collection of small, independent services.
- JPA (Java Persistence API): An API for managing relational data in Java.
- JUnit: A framework for creating and running automated tests in Java.
- TDD (Test-Driven Development): A software development methodology where tests are written before the functional code.