Billing System using Spring Boot. Custom validation. Code walkthrough

Billing System using Spring Boot. Custom validation. Code walkthrough
Spring Boot Validation

Why I have chosen custom validation for registration flow rather than Spring Boot validation.

The reason is, I have to use the Following three DTO classes for creating for new Employee, Customer, and Vendor Object. These DTO act as the request Body
In these DTO classes, I have defined another DTO class called UserDTO. This DTO class is having all attributes for creating Employee, Customer, and Vendor. Since it is a sub-object of all the above DTO classes, I cannot use the UserDTO attribute in Spring Boot Validation annotation for validation. I can use Spring Boot Validation annotation in the direct Request attributes.

So, I have created a class called which acts as Component Class and uses to do validation for EmployeeDTO, VendorDTO, and CustomerDTO’s mandatory fields.

 In UserValidator class I have used Generic for validating different Objects.
public class UserValidator<T> {
public void validate(T t) {
…………. Code implementation for validation

This “validate()” method can accept any type of class. if the given class is a type of Employee or Vendor, first I am doing User level validation which is common for all both Employee and Vendor. if any validation is found, I am collecting the error message and store it in a “validationMessages” ArrayList, then I am validating Employee specific validation, if anything found, I am adding it in the same “validationMessages” ArrayList. Now we have a validation error message is available in “validationMessges” ArrayList.

After completion of the validation, I am checking if “validationMessages” ArrayList contains the error messages if an error message exists,I am throwing InvalidInputException, Once throw the error message, Spring Boot’s centralized exception framework, capture the error message and throws to the Client-side as an Error Response.

Please check the following YouTube link for a better understanding of Custom Validation.

You can download the code from following GitHub Repository

Please follow our ITGARDEN YouTube channel for Billing System Project development work using Spring Boot. The Project source code absolutely free. This project is very good for who is looking for real-time project experience in Spring Boot. To understand the Billing System Project code, watch all Billing System related YouTube videos from this link. Click Here
Our YouTube Channel
Please follow me on for all billing system related stories and Java-based technical Articles
Note: To find all billing System related stories find “billing system itgarden” in

Close Menu