Custom Class-Level Constraint Validator in Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back in our previous video we saw how to create a crossfield validator in angular front-end application today in this video i am going to show you how to implement a custom class level constraint validator in springboot backend application as usual i am going to use a sample application called task app for the use case scenario we are going to compare two attributes in the same dto class and then we will decide whether the object is valid or invalid we will be creating a custom annotation a custom validator class and then we will use it in our data transfer object class so let's get started so in our previous video we saw how to restrict an user from creating a task with due date greater than or equal to repeat until date but still an user who has an access to a backend rest api will be able to make a post request or put request with invalid due date and repeat until date for an example this is vega ui of our backend application and this is our task controller with different end points like getting a task creating a task and updating a task and deleting it as let's say i want to uh make a post request that is to create a task with invalid due date and repeat until date so this is our json object with due date 31st of january and repeat until date i have set it to 20th of january so let's hit execute now as you can see here we have got the 200 uh as response score and response body which means it has created although the due date and repeat until date is invalid so that should not happen we are going to create a constraint which is going to restrict user from uh creating a task with such uh invalid due date and repeat until date so let's see how to implement this valid data in our backend application so this is a task data transfer object with different attributes in it as you can see here it already has a field level constraints on on each attributes for example due date has a future or present repeat until as at future and these are inbuilt constraints so now we are going to create a custom constraint which is going to compare the due date and repeat until date and then decide whether the complete object is valid or invalid so the first step is to include a dependency in a form.xml file and the dependency is called spring boot starter validation i have already included it so let's move on to next step the next step is to create a custom annotation let's go ahead and create an annotation called valid task detail so valid task ttv file is created and now we are going to add couple of annotations to this file here constrained and in this annotation we will be adding a validator class which we'll be creating in our next step and then a target annotation here you will be writing element type the different element types and this element type says that in which place we are going to use this annotation for example when element type is parameter will be using this annotation for the parameters or method or only fields in our case we are going to apply this annotation to the whole class so we have to choose element type dot type and then comes retention policy and we want this annotation to be available throughout the runtime so retention policy is on and now you have to include some boilerplates like this here and the important one is the first line here this is the message except that the default value as due date should not be greater than or equal to repeat until date which means when the task d2 object is invalid because of due date and repeat until date then we are going to get this message by default so let's go ahead and create a validator class and then include a validator class here so let's create a validator class called task dto data and this custom validator is going to implement constrained validator and here comes the annotation name valid task dtv and then the object on which our annotation is going to be applied and let's implement some of the methods of constraint validator this both and hit ok so i've created a custom validator and implemented which implements a constraint constraint validator and this this is a method where we are going to write a condition which decides whether the class is valid or invalid so the condition is we are going to check if the task tto the reference incoming reference type is none if it is none then i am going to return true which means um when the task data requires type is none then i say the task video is valid otherwise i'm going to say i'm going to check that the task to do written until date is not null and let us attach data has a due date which is before the task ddos repeat until date and this condition is satisfied and impedance true or false which decides whether the task video is valid or invalid so our custom task studio validator is ready and we have to include this task validator in our annotation task tdo validator dot class that's it so these two files are important ones which we need to implement our custom value data let's go ahead and include this uh annotation in our ttu class [Music] that's it now most importantly we have other step which is we have to include a valid annotation to our uh controller method which means for example here when when i send a post request this method will get fired with the request boarding body task rate dto which is a child class of our ddo task td and here we have to annotate this incoming parameters with add value only then the validation will get invoked so now uh i will build our application and let's see how it works so i have built a application and the task backend container is has been recreated and it's up and running let's go ahead and create a task with invalid due date and repeat until date so in our post request we are going to send a request body with due date 31st of january and it's going to be repeated daily until 20th of january which is which is an invalid date the due date should not be greater than repeat until date so i am expecting an error message saying that due date is greater than should not be greater than repeat until date so i have used a global exception handler called risk with rest control advice and once it finds that our ddo object is invalid then our method called the handle method argument not valid will get will get called and method argument not valid exception will be thrown so let's hit execute so you made a post request to create uh task with invalid due date and repeat until date so as you can see here we have received a http status 400 with the bad request and the message is due rate should not be greater than or equal repeat update so that's how we can compare the two fields and then decide whether the class is valid or invalid using our class level custom constraint validator thank you very much for watching and see you guys on my next video
Info
Channel: LumaBIT
Views: 2,175
Rating: undefined out of 5
Keywords: spring boot, java, spring, spring boot tutorial, dto, @valid spring boot, class level constraint, rest controller in spring boot, zoneddatetime java 8, postmapping spring boot, custom validator spring boot rest api, custom validator spring boot, angular, custom validator angular, custom annotations in spring boot, zoneddatetime, zonedda, spring boot with angular, springfox swagger2 example, springfox swagger ui, springfox swagger maven, Data Transfer Object spr
Id: 6pkaYjJVlyk
Channel Id: undefined
Length: 11min 37sec (697 seconds)
Published: Tue Jan 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.