@Digits Javax Validation Hibernate Spring Boot Example

In this post, we will see @Digits Javax Validation Hibernate Spring Boot Example Using Oracle Database. We will see how to use @Digits annotation with BigDecimal type filed, also we will define an error handler for the validation error message.

@Digits Javax Validation Hibernate Spring Boot Example

  • @Digits annotation has been defined in javax.validation.constraints package and the field where we are going to use this annotation must be a number.
  • We need to use mandatory attributes integer and fraction with @Digits. For example @Digits(integer = 5, fraction = 2).
  • This annotation support following types – BigDecimal, BigInteger, CharSequence, int, long, byte, short, Integer, Long, Byte and Short.

Student.java

 

Valid Request Data –

@Digits Javax Validation Hibernate

Invalid request data –

@Digits Javax Validation

@Digits Javax Validation Hibernate Spring Boot Example from scratch.

After running below example, we will be able to save an entity in the database if we have valid request data else it will show the validation error message.

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 SpringMain class(run as java application).

 

That’s all about @Digits Javax Validation Hibernate Spring Boot Example.

You may like.

See Docs.

Top