The second requirement is email should not be null or empty okay so now let’s go ahead and let’s add an annotation that is not empty annotation and also the email field should validate the email format for that we have to add email annotation from java x dot validation dot constraints package let’s choose it now let’s go ahead and let’s add validation for password let’s say the client requirement for validating password is like password should not be null or empty so to do that we’re going to use again add null at not empty annotation.
Let’s say if the password should have at least eight characters okay now we’re gonna use add size annotation and then it has a mean attribute and we can specify min equals to eight and we can give a message here something like this the customer edition message password should have at least eight characters it’s pretty simple isn’t it great now we have uh you know added although validation to user bin.
The next step is we need to add a valid annotation to the user controller so go to the create user method here and along with at request body annotation we’re going to add valued annotation so make sure that you choose to add valid annotation from java x dot validation package, so this annotation basically enables to hibernate validator and other java API validations that we have added in user bin, okay, so this is the second step make sure that you add valid annotation along with at request body annotation in creating user method so we are going to basically uh you know validate create user rest API request.
Only two steps once you create a rest API then you need to you know first add all you know annotations like java bin annotations this is the first step second step we need to enable the annotation by using add valid annotation, okay that’s it now let’s go ahead and run springboard application and let’s see how it works go to the spring boot main class right-click or run as springboard application all right our spring boot application is up and running on embedded tomcat server on 8080. Let’s head over to the postman client.
Let’s quickly test the rest of API let’s create a new request in postman client and let’s type the URL localhost 8080 slash API slash users and select HTTP method post and go to the body section and choose raw here and content type JSON and here we need to create a JSON request name let’s say Ramesh email ramesh.gmail.com and then password something like password123 and hit send button and here we go the record is successfully stored in a database that is h2 database and we got a response of the rest API now what we’re going to do is we’re going to validate the request let’s remove the name and keep an empty value for the name field and if we hit the send button and here.
We got a response it says that the bad request and we didn’t get a message here okay so we have to customize the validation response, okay this is not a good appropriate response, so this is a springboard default response for validation but we need to remove this response and we need to add our own custom response validation and if you look at the console where we can see the messages but in response, we are not able to see the messages so what we are going to do is we are going to add a custom you know code to a validation response for that what I want to do is I want to create a new class and let’s call it as validation handler something like this and extend this class with response entity exception handler and let’s override one of the methods.
Let’s override one of the methods so let me let me show you which method you need to override, so this is the handle method uh argument not valid, so this is the method we need to override just paste the method and hit ok now we have already in the method that handles method argument, not valid method now I’m going to write our own custom logic here all right so let me quickly create a map we will return basically all the error messages to the client for that we can use a map to store key-value here key as a string value as registering and errors equals to new hash map perfect now we have method argument not valid exception okay this object.
We can get all the errors to let’s call get the binding result and then get all errors so get all errors’ method returns a list so we can use for each method and each method has a consumer function interface we can use a lambda expression here to implement consumer interface okay and the argument here we can say error and let’s get a field name and a default message from the error object so here I just cast this object with Hillary error class okay now I can call get filled method here, so this method returns a field name and you can take this field name in a string let’s say field name.
Similarly, let’s get an error message or the validation message from the error object error dot get default message now let’s put field name as a key and message as a value in map errors dot put and key as a field name and value as a message and now simply written new and then response entity object and in a body just pass a map that is errors and status as HTTP status dot bad request okay that’s it pretty simple now let’s go ahead and let’s add an annotation to this class that is at the read controller advice annotation so we use add controller advice annotation to handle validation and exception in a single place.
Let’s save the file and let’s go to the spring boot main class right click run as springboard application okay let’s head over to postman client and once again send the request and here we go we got to customize a varied response like username should have at least two characters and email must be well permitted we have passed a wrong email ID and we have pass empty value to null value to name field that’s why we got a response like this okay it looks good right for each field we need to we can see the validation message.