It's actually a way to qualify custom annotations by @constraint.
@Constraint (Validatedby = xxxx.class)
Here's what I do. Java Custom Annotations code sample for pre and post parameter verification
Interested in this, please look good, learn well:
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: The custom annotation realizes the front and rear parameter verification, determines whether contains the illegal character * @author nameless * @date 2016-7-25 afternoon 8:22:58 * @ve Rsion 1.0 */@Target ({Elementtype.field, elementtype.method}) @Retention (Retentionpolicy.runtime) @Constraint ( Validatedby = IsValidString.ValidStringChecker.class) @Documented public @interface isvalidstring {String message () de
Fault "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, through @constraint (Validatedby = IsValidString.ValidStringChecker.class) The method logic that defines the annotation---the inner class named Validstringchecker for the annotation class.
And the inner class implements the Constraintvalidator<isvalidstring,string> interface
The official document describes this:
Javax.validation
Interface Constraintvalidator<a extends Annotation,t>
•
------------------------------------------------
Public interface Constraintvalidator<a extends Annotation,t>defines-logic to validate-given a for-a Given object type T.
Implementations must comply to the following restriction:
T must resolve to a non parameterized type
or generic parameters of T must be unbounded wildcard
The annotation supportedvalidationtarget can is put on a constraintvalidator implementation to mark it as supporting cross -parameter constraints. Check out Supportedvalidationtarget and Constraint for the more information.
The IsValid method of implementation is the verification method of the interface.
To test the effect, add a note to the Entity class field you want to verify:
Write the article page, the title of the article add ' < ' and then submit:
Commit failure, report 500 error, note entry is valid:
But this still has the question, my blog website cannot print out the error information directly. Still have to get an error page out.
This simple, web.xml add the error page path, and then do a page can:
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
First, introduce some basic concepts:
1.java defines an annotation with @interface xx{}.
Annotation of this thing, in fact, is not mysterious, but a mark, the program to run to the mark, the implementation of the corresponding logic. The annotation itself is a class.
2. Annotations, when defined, can indicate a particular meaning by marking some annotations:
@Retention (Retentionpolicy.source)//annotations exist only in the source code and are not included in the class byte code file
@Retention (Retentionpolicy.class)//default retention policy, annotations exist in the CLASS bytecode file, but cannot be obtained at run time
@Retention (retentionpolicy.runtime)//annotations will exist in the class bytecode file and can be obtained by reflection at runtime
(Runtime is worth noting because it means that it can be reflected to get it)
@Target (Elementtype.type)//interfaces, classes, enumerations, annotations
@Target (Elementtype.field)//field, enumerated constants
@Target (Elementtype.method)//method
@Target (Elementtype.parameter)//Method parameters
@Target (Elementtype.constructor)//constructor
@Target (elementtype.local_variable)//local variable
@Target (Elementtype.annotation_type)//annotations
@Target (Elementtype.package)//Package
One approach is to add @taget (XX) and @retention (retentionpolicy.runtime) to the definition of annotations, but not to write the method in the annotation, only to get the annotation at run time through the reflection mechanism, and then write the corresponding logic (the so-called annotation parser).
It's probably a similar notation:
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;
public boolean isnotnull () default true;
}
After running, use reflection to get annotations, specifically not to talk about.
Previous search on the Internet to find the technical articles are this, to the time I brought a lot of confusion. Think it's not what I want.
Above this Java custom annotation realizes the front and back table parameter Verification example is the small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.