Class ApiResponseAutoConfiguration
This configuration is automatically loaded by Spring Boot's autoconfiguration mechanism when the library is present on the classpath. It registers essential beans required for the library to function properly, including the comprehensive global exception handler.
Zero Configuration Required: Simply adding the library dependency enables all features
automatically. No manual @ComponentScan or @Import annotations are needed.
What Gets Auto-Configured:
GlobalExceptionHandler- Comprehensive exception handling with RFC 9457 ProblemDetail format- 10 built-in exception handlers covering all common error scenarios
- Automatic trace ID generation and logging
- Validation error aggregation and formatting
- Production-ready error messages
How It Works:
Spring Boot 3.x+ automatically reads META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
and loads this configuration class during application startup.
Disabling Auto-Configuration:
If you need to customize or disable this autoconfiguration, you can exclude it in your main application class:
@SpringBootApplication(exclude = ApiResponseAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Or in application.properties:
spring.autoconfigure.exclude=io.github.og4dev.config.ApiResponseAutoConfiguration
Alternatively, disable the exception handler while keeping other library features:
api-response.enabled=false
- Since:
- 1.0.0
- Version:
- 1.0.0
- Author:
- Pasindu OG
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for ApiResponseAutoConfiguration. -
Method Summary
Modifier and TypeMethodDescriptionRegisters theGlobalExceptionHandleras a Spring bean for automatic exception handling.
-
Constructor Details
-
ApiResponseAutoConfiguration
public ApiResponseAutoConfiguration()Default constructor for ApiResponseAutoConfiguration.This constructor is automatically invoked by Spring's dependency injection container during application startup when autoconfiguration is enabled.
-
-
Method Details
-
apiResponseAdvisor
Registers theGlobalExceptionHandleras a Spring bean for automatic exception handling.The handler provides comprehensive centralized exception management using Spring's
RestControllerAdvicemechanism, automatically converting 10 different types of exceptions to RFC 9457 ProblemDetail responses.Exception Coverage:
- General exceptions (500 Internal Server Error)
- Validation errors with field-level details (400 Bad Request)
- Type mismatch errors (400 Bad Request)
- Malformed JSON requests (400 Bad Request)
- Missing required parameters (400 Bad Request)
- 404 Not Found errors
- 405 Method Not Allowed
- 415 Unsupported Media Type
- Null pointer exceptions (500 Internal Server Error)
- Custom ApiException instances (custom status codes)
Features:
- Automatic trace ID generation for all errors
- Consistent trace IDs between logs and responses
- RFC 9457 compliant error format
- Comprehensive SLF4J logging with appropriate severity levels
- Automatic timestamp inclusion in all error responses
- Returns:
- A new instance of
GlobalExceptionHandlerregistered as a Spring bean.
-