Spring Boot Custom Bean Validations with Jakarta ConstraintValidator, Grouping Validation Constraints, GroupSequence and i18n messages
This project demonstrates the implementation of custom bean validations in a Spring Boot application. It focuses on validating mobile phone information, particularly IMEI numbers, using Hibernate Validator and Spring Boot.
Spring Boot allows developers to define custom bean validations by creating custom constraint annotations and implementing constraint validators. This approach provides flexibility in defining validation rules tailored to the application's requirements.
.
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── maoudia
│ │ ├── AppConfig.java
│ │ ├── AppValidator.java
│ │ ├── Application.java
│ │ ├── Full.java
│ │ ├── Heavy.java
│ │ ├── IMEI.java
│ │ ├── IMEIValidator.java
│ │ ├── Lite.java
│ │ └── MobilePhone.java
│ └── resources
│ ├── messages.properties
│ └── messages_fr.properties
└── test
└── java
└── com
└── maoudia
├── LocalizationTests.java
└── MobilePhoneValidationsTests.java
- Spring configuration class defining beans for message source and validator factory.
- Sets up the message source for localization and validator factory for validation.
- Contains properties for the application configuration.
- Spring component providing a generic validation method to validate objects using Hibernate Validator.
- Configured with a local validator factory bean to perform validations.
- Interface defining a validation group for full validation.
- Interface defining a validation group for heavy validation.
- Annotation interface marking fields to be validated as IMEI numbers.
- References the IMEIValidator class for validation.
- Custom constraint validator for validating IMEI numbers.
- Checks the IMEI's format and performs the Luhn algorithm to ensure validity.
- Interface defining a validation group for lite validation.
- Java record class defining the structure of a mobile phone.
- Includes fields such as brand, model, screen size, battery capacity, network, IMEI, and MAC address.
- Fields have corresponding validation annotations from Hibernate Validator.
- Files containing error messages for localization in English and French, respectively.
- JUnit test class demonstrating localization testing by checking localized error messages for IMEI validation with English and French locales.
- JUnit test class containing tests for validating the MobilePhone class using the AppValidator component.
To use the application:
- Define a
MobilePhone
object with the desired mobile phone information. - Pass the
MobilePhone
object to theAppValidator
for validation. - Handle any validation errors appropriately based on the business logic of your application.
- Ensure you have JDK and Apache Maven installed.
- Execute
mvn test
command to run all tests. - Review the test results to ensure all validations pass as expected.
This project provides a framework for validating mobile phone information, specifically IMEI numbers, using Hibernate Validator and Spring Boot. It demonstrates the usage of Spring Boot Custom Bean Validations, allowing developers to define custom validation rules for domain objects. Developers can customize and expand this framework to suit the validation needs of their applications.
- https://www.baeldung.com/javax-validation-groups
- https://reflectoring.io/bean-validation-anti-patterns
- https://reflectoring.io/bean-validation-with-spring-boot
- https://www.baeldung.com/spring-custom-validation-message-source
- https://dyrk.org/tools/imei
- https://www.geeksforgeeks.org/program-check-valid-imei-number