Skip to main content

Posts

Showing posts with the label advice

How to implement Global Exception Handler in Spring Boot

Exception Handling There is always a requirement to handle all the NFR (Non-Functional Requirements) well in any application so that the application performs well in production and possess the excellent traceability and exception reporting mechanisms for better visibility on what happens in applications. Types of Exceptions We normally use exceptions within each domain of the application like Customers, Orders, Inventory etc. There are also a lot of custom exceptions that might be required when the application is built to capture, report the exceptions to the user. In this process, we can follow the below given process Base Exceptions There are multiple use-cases where we need separate exceptions to report multiple error conditions or cases, let us consider one such case for data validation. BaseValidationException import java.util.List; public class BaseValidationException extends RuntimeException { public BaseValidationException() { super(); } public BaseVali

Spring boot Aspect Oriented Programming

What is AOP AOP stands for Aspect Oriented Programming. It is mainly used to implement the Cross-Cutting concerns in applications. These are used to reduce the mix of the business logic from the NFR (Non Functional Requirements) like logging, performance monitoring, validations etc. Also, these "Aspects" can be [should be] generalized and be located in a common library or package so that it can be easily upgraded / patched. Further, these Aspects are to be implemented without bringing-in these Aspects into every service or business logic layer so that they can be less invasive and like autowired to get the job done without being omni-present. Spring AOP helps us achieve this model by bringing in the support for the Aspects and help us wire these aspects at places where required. Core Terms / Concepts Aspect This is the cross cutting concern like logging / time measurement / validation etc. Join Point The method execution. (Example: Executing a service method like CustomerServ