@Constraint
It is a meta annotation in Java used to define custom verification annotations. It is usually used with Hibernate Validator or other JSR 380-based verification frameworks. pass@Constraint
Annotation, you can create your own verification logic and apply it to fields in the entity class.
1. Create custom verification annotations
First, define a custom verification annotation. Suppose we want to create an annotation@ValidLength
, the maximum length of the string is 20 bits.
import ; import ; import ; import ; import ; import ; @Constraint(validatedBy = ) @Target({ , , ElementType.ANNOTATION_TYPE, }) @Retention() public @interface ValidLength { String message() default "The length must be between 1 and 20 characters"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; int min() default 1; int max() default 20; }
2. Create a checker
Next, create an implementationConstraintValidator
Interface classes are used to implement specific verification logic.
import ; import ; public class ValidLengthValidator implements ConstraintValidator<ValidLength, String> { private int min; private int max; @Override public void initialize(ValidLength constraintAnnotation) { = (); = (); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value == null) { return true; // Allow to be empty } return () >= min && () <= max; } }
3. Apply custom verification annotations
Use custom verification annotations in entity classes.
import ; public class User { @NotNull @ValidLength(min = 1, max = 20) private String username; // Getters and Setters public String getUsername() { return username; } public void setUsername(String username) { = username; } }
4. Test verification
Write a test class to verify that the verification logic works as expected.
import ; import ; import ; import ; import ; public class Main { public static void main(String[] args) { User user = new User(); ("12345678901234567890"); // 20 digits, legal ValidatorFactory factory = (); Validator validator = (); Set&lt;ConstraintViolation&lt;User&gt;&gt; violations = (user); for (ConstraintViolation&lt;User&gt; violation : violations) { (()); } if (()) { ("Check passed"); } else { ("Check failed"); } } }
Output
After running the above code, if the length of username is between 1 and 20 characters, the output will be:
Verification passed
If the length of username is more than 20 characters or less than 1 character, the output will be:
The length must be between 1 and 20 characters
Verification failed
Summarize
-
Custom verification annotations:use
@Constraint
Annotation definition custom verification annotations. -
Verification device:accomplish
ConstraintValidator
Interface, write specific verification logic. - Application Notes: Use custom verification annotations on fields in entity class.
-
Test verification:use
Validator
The object is checked and the verification result is processed.
This is the end of this article about @Constraint implementing custom verification annotations in Java. For more related Java custom verification annotations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!