Javatpoint Logo
Javatpoint Logo

Implementing Validations for RESTful Services

The validation is a common requirement in all the services. We will discuss Java Validation API to add validation in our beans file. When we get a request to create a user, we should validate its content. If it is invalid, we should return a proper response.

Let's see how to validate a request.

Step 1: Open the UserResource.java file.

Step 2: Add @Valid annotation. It is a Javax validation API. Its default classpath is spring-boot-starter-web.

UserResource.java

Now we will add validations in User class on name and date of birth. Let suppose that name should have at least five characters and date of birth should be in past not in present.

Step 3: Open the User.java file.

Step 4: Add @Size(min=5) annotation just above the name variable.

Step 5: Add @Past annotation just above the dob variable.

User.java

Step 5: Open the Rest client Postman and send a POST request with new user name Tony k. It returns Status: 201 Created.

Implementing Validations for RESTful Services

Now we send another POST request. But the name should have less than five characters. It returns the Status: 400 Bad Request.

Implementing Validations for RESTful Services

When we create a RESTful services we need to think about consumer that how does the consumer know what is wrong. To resolve this problem we will add a method handleMethodArgumentNotValid() which is defined in the ResponseEntityExceptionHandler class. This is the method which is fired when a bad request occurs.

Step 6: Copy and paste the above method in CustomizedResponseEntityExceptionHandler.java file.

Step 7: Override the method by adding the annotation @Override.

CustomizedResponseEntityExceptionHandler.java

Step 8: Now, we send a POST request through Postman. It returns the exception structure with message Validation failed for argument and other details.

Implementing Validations for RESTful Services

It is difficult to understand the message for the user. So we will now customize the message with the string Validation Failed instead of getting message.

Step 9: Again send a POST request. It returns the message which we have customized.

Implementing Validations for RESTful Services

It might be useful for the consumer. Now we customize the message again and make it more error specific.

Step 10: Open User.java file and add an attribute message="Name should have at least 5 characters" in @Size annotation.

Step 11: Again, send a POST request. It returns the more specific exception which we have specified.

Implementing Validations for RESTful Services

We can further customize the exception by following the BindingResult interface. There is a wide variety of exception messages. There are following validation classes defined in validation-api-2.0.1.Final.jar.

Implementing Validations for RESTful Services






Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA