Hibernate Validator Constraints Example Using Spring Boot

In this post, we will see Hibernate Validator Constraints Example Using Spring Boot. Suppose we have an entity and we want to validate fields defined in the entity, we can use validators with fields. We are going to use validation-api-2.0 and hibernate-validator-6.0.9.  The validation-api 2.0 also Known as JSR-380 i.e Java Specification Requests -380) .

 

Hibernate Validator

 

Let’s see a sample example of an entity where we are using different annotations for the fields validation.

Before going ahead let’s see what we are going to do. We will define a global error handler for error messages using @ControllerAdvice and @Exceptionhandler. Also, we will have one REST API which will use to save the data in the database. If we have a valid request we will able to save data in the database else it should show error messages.

REST API – http://localhost:9091/student/save

Valid Request Data –

 

Response data –


 

 

Invalid Request Data – Make name attribute empty, give rollNumber more than 10 character, give marks more than 1000 , provide a phoneNumber more than 10 digits and also don’t give @ in emailid.

 

Response data –


 

Let’s see complete Hibernate Validator Constraints Example Using Spring Boot from scratch.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details as below and click on finish.

Hibernate Validator Spring Boot

 

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Directory structure –

Hibernate Validator Example

 

Student.java

StudentController.java

StudentRepository.java – interface

StudentService.java – interface

 

StudentServiceImpl.java

 

GlobalErrorHandler.java – Let’s define a error handler class. This class will be used when validateEntity() method will throw ConstraintViolationException(See more details about GlobalErrorHandler here).

 

ResponseError.java

SpringMain.java

 

JpaConfig.java

application.properties

Let’s run the example.

Hibernate Validator Example Using Spring Boot

 

Let’s see some important validation constraints which have been defined in validation-api-2.0. These annotations have been defined in javax.validation.constraints package. Apart from this, it has been defined some more validation constraints which we will see later.

@NotEmpty – If we use this annotation with some field that field must not be null nor empty.

@Size – If we use this annotation with some field that field size must be between the specified boundaries (included).

@NotNull – If we use this annotation with some field that field must not be null.

@NotBlank – If we use this annotation with some field that field must contain at least one non-whitespace character.

@Email – If we use this annotation with some field that field should well-formed email address.

@Max – If we use this annotation with some field that field must be a number whose value must be lower or equal to the specified maximum.

@Min – If we use this annotation with some field that field must be a number whose value must be higher or equal to the specified minimum.

@Digits – If we use this annotation with some field that field must be a number within accepted range Supported types are BigDecimal BigInteger CharSequence byte, short, int, long, and their respective wrapper types.

@Null – If we use this annotation with some field that field must be null.

@AssertFalse – If we use this annotation with some field or property that field must be false.

@AssertTrue – If we use this annotation with some field or property that field must be true.

@DecimalMax – The value of the field or property must be a decimal value lower than or equal to the number in the value element.

@DecimalMin – The value of the field or property must be a decimal value greater than or equal to the number in the value element.

@Future – The value of the field or property must be a date in the future.

@FutureOrPresent – The value of the field or property must be a date or time in present or future.

@Negative – The value of the field or property must be negative.

@NegativeOrZero – The value of the field or property must be negative or zero.

@Past – The value of the field or property must be a date in the past.

@PastOrPresent – The value of the field or property must be a date or time in the past or present.

@Pattern – The value of the field or property must match the regular expression defined in the regexp element.

@Positive – The value of the field or property must be a positive number.

@PositiveOrZero – The value of the field or property must be a positive number or zero.

 

Apart from the above annotations, in org.hibernate.validator.constraints package we have some more annotations.

@Length – Used to validate the length of fields.

@CreditCardNumber -Used to validate credit card number.

@Range -We can give a range of fields. Apply on numeric values or a string representation of the numeric value.

@URL – Used to validate URL.

See here some more Hibernate Validator Constraints which belongs to org.hibernate.validator.constraints package.

That’s all about Hibernate Validator Constraints Example Using Spring Boot.

 

You may like –

See Docs.

Top