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
EmployeeDTO,
VendorDTO,
CustomerDTO,
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 UserValidator.java 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.
@Component
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.
https://youtu.be/-5RlaJ1n5iU

You can download the code from following GitHub Repository
https://github.com/sureshstalin/billingsystem


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
https://www.youtube.com/channel/UChj5CeuWaHIFr4RkXoG3iJA?view_as=subscriber
Please follow me on Medium.com for all billing system related stories and Java-based technical Articles
https://suresh-stalin.medium.com/
Note: To find all billing System related stories find “billing system itgarden” in medium.com

Close Menu
×
×

Cart