Signup/Sign In

Spring Boot Validations

In this tutorial, we will be discussing about the spring boot validations and its implementation. Validations are used to put restrictions on the fields like their values, size, range, etc. Most of the validations are usually done at the frontend itself using Javascript but spring boot also provides a way of server-side validations.

Spring Boot Validation Implementation:

Let us change the Teacher.java in the com.tutorial.studytonight.pojo which we created previously as follows:

public class Teacher {
    private int id;
    private String name;
    private LocalDate joiningDate;
    private String institute;
    private String subject;

    //Setters and Getters
}

The javax.validation package provides many validation annotations which enable us to validate the fields. Modify the Teacher.java with the below code:

public class Teacher {

    @Size(max=10,min=5,message="criteria not met")
    private String name;

    @Size(min=3)
    private String institute;

    @Max(value=10)
    private int id;

    private String subject;

    @Past
    private LocalDate joiningDate;

    //Setters and Getters
}
  • @Size annotation is used to restrict the filed length to a specified value. It has attributes such as max and min which are used to set the maximum and minimum values respectively. The message attribute in this annotation is used to display a default message on validation failure.
  • @Max annotation is used to limit the value of a field. In the above code, the id field can have the maximum value of 10.
  • @Min annotation is used to limit the value of a field to a minimum value.
  • @Past and @Future are applied to Dates to make the sure that the data is in past or future respectively.

In the above figure, we are sending the Teacher details using POSTMAN. Notice the 400 bad request at the bottom right corner. When we compare the validations mentioned in Teacher.java class with the JSON structure in postman, the field name should be atleast 5 characters and so it resulted in 400 bad request.

The above figure represents the Response body of the 400 Bad request. Notice the default message "criteria not met", This was displayed as the attribute message was set while validating the name field in Teacher.java class. Let us test the API which fulfills all the validations.

It can be seen that the above request has successfully returned the 200 OK status. ResponseEntityExceptionHandler has a method which will be invoked upon the validation failure. We have created a CustomizedExceptionHandling in the pacakage com.tutorial.studytonight.exception which extends the ResponseEntityExceptionHandler class. We need to override the method of ResponseEntityExceptionHandler class as follows:

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {

    System.out.println("Validation failed");
    return super.handleMethodArgumentNotValid(ex,headers, status,request);

}

Let us test the API again as shown below.

As the request failed in the above figure, it invokes the override handleMethodArgumentNotValid method displaying the console message as shown below:

Conclusion: In this tutorial, we have seen the validation implementation using different annotations in spring boot.



About the author:
I'm a writer at studytonight.com.