A reusable Spring Boot library for centralized exception handling.
This library provides a common Global Exception Handler that can be used across multiple Spring Boot projects as a Maven dependency.
- Avoid writing duplicate
@ControllerAdvicein every project - Standardize API error responses
- Handle validation, business, and system exceptions centrally
- Global exception handling using
@ControllerAdvice - Supports validation errors (
@Valid) - Supports custom business exceptions (
ApiException) - Handles runtime and system exceptions
- Auto-configured using Spring Boot auto-configuration
<dependency>
<groupId>com.icodetech</groupId>
<artifactId>sbglobalexceptionhandler</artifactId>
<version>1.0</version>
</dependency>- This library uses Spring Boot AutoConfiguration.
- You do NOT need to add any @ControllerAdvice in your project.
- Auto-configuration class:
GlobalExceptionAutoConfiguration
📌 Application.java
@SpringBootApplication
@EnableGlobalExceptionHandling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}1️⃣ Validation Errors
- Triggered when @Valid fails.
- Example DTO:
public class TestRequest {
@NotBlank(message = "name is mandatory")
private String name;
@NotNull(message = "age is required")
private Integer age;
} - Controller:
@PostMapping("/validate")
public String validate(@Valid @RequestBody TestRequest request) {
return "SUCCESS";
} - Response:
{
"responseCode": 400,
"responseMessage": "Validation Errors",
"errors": [
{
"fieldName": "name",
"message": "name is mandatory"
},
{
"fieldName": "age",
"message": "age is required"
}
]
}2️⃣ Business Exception (ApiException)
- Throw anywhere in your code:
throw new ApiException(HttpStatus.BAD_REQUEST, "Invalid business rule");
- Response:
{
"responseCode": 400,
"responseMessage": "Invalid business rule",
"errors": {
"message": "Invalid business rule"
}
}3️⃣ System / Runtime Exception
- Example:
String value = null;
value.toString();
- Response:
{
"responseCode": 400,
"responseMessage": "Error occurred",
"errors": {
"message": "Cannot invoke \"String.toString()\" because \"value\" is null"
}
}🚫 What NOT to Do
- ❌ Do NOT create your own @ControllerAdvice
- ❌ Do NOT override exception handling in consumer projects
The library handles everything automatically.
Enable logs:
logging.level.org.springframework.boot.autoconfigure=DEBUGStartup log should contain:
GlobalExceptionAutoConfiguration matched
- ApiException
- MethodArgumentNotValidException
- HttpMessageNotReadableException
- MissingServletRequestParameterException
- MethodArgumentTypeMismatchException
- TypeMismatchException
- NoHandlerFoundException
- SQLIntegrityConstraintViolationException
- RuntimeException
- Exception
- Java 8+
- Spring Boot 2.7.x
- Maven
This project is licensed under the Apache License, Version 2.0.
iCode Technology
- Spring Boot 3.x support
- Error code enum support
- Property-based enable/disable
- RFC 7807 (Problem Details) support
- Logging & trace-id integration