1. Scenario Analysis
When we use SpringMVC to verify the ID number data at the Controller layer, we often use the following methods:
@RestController @RequiredArgsConstructor @RequestMapping("member") public class MemberController { // Regular expression of ID number String regex = "^(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$)$"; @PostMapping("/register") public R<Void> register(@RequestBody @Valid Member member) { Pattern pattern = (regex); Matcher matcher = (()); if (!()) { return ("Not a valid ID number"); } (member); return (); } }
Of course we can use the above method to perform data verification, but this method is not very elegant:
If there are other objects in the project that need to be checked for ID number, the same code will be scattered all over the project.
Javax Validation provides us with another elegant way to perform logically repeated data verification.
2. Code implementation
1. Create custom verification annotations
First create a custom verification annotation to verify whether the string is in a valid ID number format
package ; import ; import ; import ; import .*; @Target({, , ElementType.ANNOTATION_TYPE, , , ElementType.TYPE_USE}) @Retention() @Documented @Constraint( validatedBy = {} ) public @interface IdCheck { String message() default "Not a valid ID number"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
2. Create a verification device implementation class
Create a class that implements the ConstraintValidator interface to implement custom verification logic
package ; import ; import ; import ; import ; import ; public class IdCheckValidator implements ConstraintValidator<IdCheck, String> { // Regular expression of ID number String regex = "^(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$)$"; private Pattern pattern; @Override public void initialize(IdCheck constraintAnnotation) { // Initialize pattern = (regex); } @Override public boolean isValid(String idNo, ConstraintValidatorContext constraintValidatorContext) { Matcher matcher = (idNo); return (); } }
3. Use custom annotations in entity classes
package ; import ; import ; @Data public class Member { // Custom annotations @IdCheck private String idNo; }
4. Perform data binding and verification in the controller
package ; import ; import ; import ; import ; import ; import ; import ; import ; @RestController @RequiredArgsConstructor @RequestMapping("member") public class MemberController { @PostMapping("/register") public R<Void> register(@RequestBody @Valid Member member) { (member); return (); } }
5. Test
Enter an erroneous ID number:
###
POST http://localhost:8080/member/register
Content-Type: application/json
{"idNo": "811111111111111111"}
Output:
{
"code": -1,
"msg": "Not a valid ID number"
}
Enter a correctly formatted ID number (this ID number was randomly generated by me):
###
POST http://localhost:8080/member/register
Content-Type: application/json
{"idNo": "12010319881011691X"}
Output:
{
"code": 0,
"msg": "success"
}
3. Summary
ConstraintValidator is an interface provided to us by the javax validation specification to implement data verification.
There are many implementations of this interface, like our common ones
- NotNullValidator
- MaxValidatorForMonetaryAmount
- MinValidatorForMonetaryAmount
All are its implementations. For the above code, refer to the implementation of NotNullValidator.
This is the article about Javax Validation custom annotations for ID number verification. For more information about Javax Validation custom annotations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!