java自定义注解实现前后台参数校验
其實是可以通過@Constraint來限定自定義注解的方法。
@Constraint(validatedBy = xxxx.class)
?
下面是我做的 java自定義注解實現前后臺參數校驗 的代碼示例
對這個感興趣的,請好好看,好好學:
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: 自定義注解實現前后臺參數校驗,判斷是否包含非法字符 * @author 無名 * @date 2016-7-25 下午8:22:58 * @version 1.0*/ @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = IsValidString.ValidStringChecker.class) @Documented public @interface IsValidString {String message() default "The string is invalid.";Class<?>[] groups() default {};Class<? extends Payload>[] payload() default{};class ValidStringChecker implements ConstraintValidator<IsValidString,String>{@Overridepublic void initialize(IsValidString arg0){ }@Overridepublic boolean isValid(String strValue, ConstraintValidatorContext context){if(StringUtill.isStringEmpty(strValue)){return true;}if(strValue.contains("<")){return false;}return true;}} }上述代碼,通過@Constraint(validatedBy = IsValidString.ValidStringChecker.class)限定了注解的方法邏輯---該注解類的名為ValidStringChecker的內部類。
而該內部類實現了ConstraintValidator<IsValidString,String>接口
官方文檔是這樣描述的:
javax.validationInterface 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 restriction:
- 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.
?
實現的isValid方法便是,該接口的校驗方法。
?
試驗一下效果,在要校驗的實體類字段加上注解:
寫文章頁面,文章標題內加入'<'然后提交:
?
提交失敗,報500錯誤,說明注解生效:
但這樣還有問題,我的blog網站不能直接打印出報錯信息。還是要搞一個error頁面出來。
這個簡單,web.xml下加入error頁面路徑,然后做一個頁面即可:
<error-page>?
<error-code>500</error-code>?
<location>/error.jsp</location>?
</error-page>
?
首先介紹些基本概念:
1.java用@interface xx{}定義一個注解。
注解這個東西,其實并不神秘,不過是一種標記,程序運行到標記處,就執行相應邏輯罷了。注解本身即是一個類。
2.注解在定義時,標注一些注解可以表示特定意義:
@Retention(RetentionPolicy.SOURCE) // 注解僅存在于源碼中,在class字節碼文件中不包含
@Retention(RetentionPolicy.CLASS) // 默認的保留策略,注解會在class字節碼文件中存在,但運行時無法獲得
@Retention(RetentionPolicy.RUNTIME) // 注解會在class字節碼文件中存在,在運行時可以通過反射獲取到
(RUNTIME的值得注意下,因為意味著可以反射來獲取)
?
@Target(ElementType.TYPE) // 接口、類、枚舉、注解
@Target(ElementType.FIELD) // 字段、枚舉的常量
@Target(ElementType.METHOD) // 方法
@Target(ElementType.PARAMETER) // 方法參數
@Target(ElementType.CONSTRUCTOR) // 構造函數
@Target(ElementType.LOCAL_VARIABLE) // 局部變量
@Target(ElementType.ANNOTATION_TYPE) // 注解
@Target(ElementType.PACKAGE) // 包
?
有一種做法就是在定義注解時加上@Taget(xx)和@Retention(RetentionPolicy.RUNTIME) ,但沒有在注解中寫方法,只是在運行時通過反射機制來獲取注解,然后自己寫相應邏輯(所謂注解解析器)
大概是類似的寫法:
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; }之后運行時,用反射獲取注解,具體不談。
之前在網上查找這方面技術文章找到的都是這種,給當時的我帶來很大困惑。覺得不是我想要的。
?
?
?
參考文章:
http://www.cnblogs.com/liangweiping/p/3837332.html
http://www.cnblogs.com/rixiang/p/5709110.html
總結
以上是生活随笔為你收集整理的java自定义注解实现前后台参数校验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自定义的注解校验器的实现
- 下一篇: 深入理解java中的Soft refer