Skip to main content


Showing posts from May, 2021

Adding Custom Validation in Lombok Builder

What is Lombok? Lombok is a Java library that auto generates lots of boiler plate code for Java classes. Lombok works at compile time and manipulates Java byte code to add additional features. Lombok uses annotations to specify what boiler plate code will be generates. Eg. @Getter annotation can be applied to any Java bean to auto generate Getter methods for all the fields in that bean. In this article we will be looking at, how to add custom validations on fields using Lombok Builder annotation What is Builder annotation in Lombok? Lombok can be used to generate Builder class for a Java bean, by annotating the bean class with @Builder annotation.  In certain cases, we might want to validate the data, in builder class, before creating the bean object. Lombok provides simple validator annotations like @NonNull to enforce nullability checks. But what if, we want to have more custom validations or want to throw custom errors. Customizing Lombok Builder class. Let's consider below Emp