In fact, it is a method that can be used to define custom annotations through @Constraint.
@Constraint(validatedBy = xxxx.class)
Below is a code example of the Java custom annotation I made to implement front and backend parameter verification
If you are interested in this, please read it carefully and learn it carefully:
package sonn.sonnannotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import javax.validation.Constraint;import javax.validation.ConstraintValidator;import javax.validation.ConstraintValidatorContext;import javax.validation.Payload;import sonn.util.StringUtill;/*** @ClassName: IsValidString * @Description: Custom annotations implement front and backend parameter verification to determine whether they contain illegal characters* @author Unnamed* @date 2016-7-25 8:22:58 pm * @version 1.0 */@Target({ElementType.FIELD, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Constraint(validatedBy = IsValidString.ValidStringChecker.class)@Documentedpublic @interface IsValidString { String message() default "The string is invalid."; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default{}; class ValidStringChecker implements ConstraintValidator<IsValidString,String> { @Override public void initialize(IsValidString arg0) { } @Override public boolean isValid(String strValue, ConstraintValidatorContext context) { if(StringUtill.isStringEmpty(strValue)) { return true; } if(strValue.contains("<")) { return false; } return true; } }}The above code defines the method logic of the annotation through @Constraint(validatedBy = IsValidString.ValidStringChecker.class)--the inner class of the annotation class named ValidStringChecker.
This internal class implements the ConstraintValidator<IsValidString,String> interface
The official documentation describes it like this:
javax.validation
Interface ConstraintValidator<A extends Annotation,T>
•
--------------------------------------------------------------------------------------------------------------------------------
public interface ConstraintValidator<A extends Annotation,T>Defines the logic to validate a given constraint A for a given object type T.
Implementations must comply to the following restrictions:
•T must resolve to a non parameterized type
• or generic parameters of T must be unbounded wildcard types
The annotation SupportedValidationTarget can be put on a ConstraintValidator implementation to mark it as supporting cross-parameter constraints. Check out SupportedValidationTarget and Constraint for more information.
The isValid method implemented is the verification method of this interface.
Test the effect and add annotations to the entity class field to be verified:
Write the article page, add '<' in the article title and submit:
Submission failed, with an error of 500, indicating that the annotation takes effect:
But there are still problems with this. My blog website cannot print out the error message directly. You still have to create an error page.
This is simple, add the error page path under web.xml, and then make a page:
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
First, let’s introduce some basic concepts:
1.java defines an annotation with @interface xx{}.
Annotation is actually not mysterious, it is just a mark. When the program runs at the mark, it executes the corresponding logic. Annotations themselves are a class.
2. When defining annotations, labeling some annotations can represent specific meanings:
@Retention(RetentionPolicy.SOURCE) // The annotation only exists in the source code and does not contain it in the class bytecode file.
@Retention(RetentionPolicy.CLASS) // The default retention policy, the annotation will exist in the class bytecode file, but it cannot be obtained during runtime.
@Retention(RetentionPolicy.RUNTIME) // The annotation will exist in the class bytecode file and can be obtained through reflection at runtime.
(RUNTIME is worth noting because it means that it can be retrieved by reflection)
@Target(ElementType.TYPE) // Interface, class, enumeration, annotation
@Target(ElementType.FIELD) // Constant for fields and enums
@Target(ElementType.METHOD) // Method
@Target(ElementType.PARAMETER) // Method parameters
@Target(ElementType.CONSTRUCTOR) // Constructor
@Target(ElementType.LOCAL_VARIABLE) // Local variables
@Target(ElementType.ANNOTATION_TYPE) // Annotation
@Target(ElementType.PACKAGE) // Package
There is a way to add @Taget(xx) and @Retention(RetentionPolicy.RUNTIME) when defining annotations, but do not write methods in the annotations. They just use reflection mechanism to obtain the annotations at runtime, and then write the corresponding logic yourself (the so-called annotation parser)
Probably a similar way to write:
import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Documented@Inherited@Target({ ElementType.FIELD, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)public @interface Validate{ public int min() default 1; public int max() default 10; public boolean isNotNull() default true;}When running later, use reflection to get annotations, and I won’t discuss the details.
I found this kind of technical articles on this before looking for technical articles on the Internet, which brought me great confusion at that time. I don't think I want.
The above example of Java custom annotation to implement front and backend parameter verification is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.