SHARED LIBRARY

Java 17 Spring Boot 3.5.5 Build

Project Overview

This shared library serves as the foundational backbone in a microservices ecosystem. It encapsulates reusable domain models, utility classes, validation groups, and robust error-handling mechanisms to ensure consistency, eliminate code duplication, and enforce best practices across both Servlet and Reactive microservices.

Technology Stack & Compatibility

Key Features

Zero-Touch Activation & Configuration

Multi-Tenancy & Context Management

Stack Support & Exception Handling

Reusable Domains & Utils

Advanced Validation

Developer Authentication Setup

Before consuming or deploying this library, one must authenticate Maven or Gradle with GitHub Packages using a Personal Access Token (PAT).

Generating Token

For Maven project: Copy the token into global ~/.m2/settings.xml file:

settings.xml
<settings> <servers> <server> <id>github</id> <username>GITHUB_USERNAME</username> <password>ghp_ACCESS_TOKEN</password> </server> </servers> </settings>

For Gradle project: Copy the token into global ~/.gradle/gradle.properties file:

gradle.properties
gpr.user=GITHUB_USERNAME gpr.key=ghp_ACCESS_TOKEN

Deployment

To publish this library to private package registry,

For Maven, add maven-deploy-plugin plugin and configure distributionManagement inside pom.xml

<distributionManagement> <repository> <id>github</id> <name>GitHub Packages</name> <url>https://maven.pkg.github.com/Kranthi0307/shared-service</url> </repository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.1</version> </plugin> </plugins> </build>

For Gradle, add maven-publish plugin and configure the publishing block inside build.gradle

plugins { id 'maven-publish' id 'java' } publishing { publications { mavenJava(MavenPublication) { from components.java } } repositories { maven { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/Kranthi0307/shared-service") credentials { username = project.findProperty("gpr.user") password = project.findProperty("gpr.key") } } } }

Run the following command to deploy securely via CI/CD pipeline:

For Maven,

mvn clean deploy

For Gradle,

gradle clean publish

Update the version before deploying.

Installation

Integrating the library is seamless. Simply add the following dependency to the hosting application:

Authentication and the source of library needs to be configured

For Maven

<repositories> <repository> <id>github</id> <url>https://maven.pkg.github.com/Kranthi0307/shared-service</url> </repository> </repositories> <dependency> <groupId>com.kranthi</groupId> <artifactId>shared-service</artifactId> <version>1.x.x</version> </dependency>

For Gradle

repositories { mavenCentral() maven { url = uri("https://maven.pkg.github.com/Kranthi0307/shared-service") credentials { username = project.findProperty("gpr.user") password = project.findProperty("gpr.key") } } } implementation 'com.kranthi:shared-service:1.x.x'

No additional annotations (like @Enable*) or explicit configuration scans are required. The library auto-configures itself upon building the application.

Prerequisites & Configuration

To provide compilation-time IDE support (like autocomplete for configuration properties) in hosting applications, ensure the spring-boot-configuration-processor is included in the build.

Intelligent Defaults: The library utilizes @ConfigurationProperties bundled with @DefaultValue to provide plug-and-play defaults (e.g., config.web.public-end-points="/actuator/health") without polluting the host application's properties file.

@ConfigurationProperties(prefix = "config.web") public record MyWebProperties( @DefaultValue("/actuator/health") String[] publicEndPoints ) { }

Project Structure

├───config/ #Framework AutoConfigurations ├───domains/ #Reusable domains, DTOs, and common models ├───exceptions/ #Abstract ecosystem RuntimeExceptions ├───handlers/ #WebMVC/WebFlux @RestControllerAdvice instances ├───properties/ #Java 17 configuration records ├───utils/ #Common thread-safe helper utilities └───validation/ #Validation groups (CreateValidation, UpdateValidation)

Reference Implementation Examples

Validation Groups

public interface CreateValidation {} public interface UpdateValidation {}

Domain

public record ResponseRecord<T>(String message, T data) { }

Exception

public abstract class BaseException extends RuntimeException { private final String errorCode; protected BaseException(String message, String errorCode) { super(message); this.errorCode = errorCode; } protected BaseException(String message, String errorCode, Throwable cause) { super(message, cause); this.errorCode = errorCode; } public String getErrorCode() { return errorCode; } }

Handler

@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(BaseException.class) public ResponseEntity<ResponseRecord<String>> handleBaseException(BaseException exception) { HttpStatus status; //My exceptions Logic return ResponseEntity.status(status) .body(new ResponseRecord<>( exception.getMessage(), exception.getErrorCode() )); } }

Error Handling

The library provides a structured, centralized error-handling architecture that standardizes API error responses ecosystem-wide without capturing unintended framework or generic runtime exceptions.

@ExceptionHandler(BaseException.class)

Crucial: Bean Naming Collisions
Since this library provisions beans automatically via @AutoConfiguration, one must prevent duplicate bean names between different stack variants (Servlet vs. Reactive).
If a hosting application attempts to build its own bean with a conflicting name (e.g., a generic globalExceptionHandler), Spring Boot's context initialization will fail.
Rule: Always namespace, auto-configured internal beans clearly (e.g., servletGlobalExceptionHandler vs reactiveGlobalExceptionHandler) within conditional configuration classes.

Local Development

To make changes to this library and test them locally in a hosting application:

mvn clean install

Decision Log (Architecture Decision Records)

ADR 1: Decoupled Architecture via Optional Dependencies

ADR 2: Avoided @PropertySource for Default Configurations

ADR 3: Auto-Configuration Split (Servlet vs. Reactive)

ADR 4: Thread-Safe State Management in Multi-Tenant

ADR 5: Decoupled Multi-Tenancy Data Dependencies

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <optional>true</optional> <scope>provided</scope> </dependency>

Troubleshooting

Future Enhancements

The following capabilities are planned for upcoming releases: