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>
{@Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initialize(IsValidString arg0) { } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isValid(String strValue, ConstraintValidatorContext context) { </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(StringUtill.isStringEmpty(strValue)) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">if</span>(strValue.contains("<"<span style="color: rgba(0, 0, 0, 1)">)) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; }
}
}
上述代码,通过 @Constraint(validatedBy = IsValidString.ValidStringChecker.class) 限定了注解的方法逻辑 --- 该注解类的名为 ValidStringChecker 的内部类。
而该内部类实现了 ConstraintValidator<IsValidString,String> 接口
官方文档是这样描述的:
Interface ConstraintValidator<A extends Annotation,T>
public interface ConstraintValidator<A extends Annotation,T>
Defines the logic to validate a given constraintA
for a given object typeT
.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 aConstraintValidator
implementation to mark it as supporting cross-parameter constraints. Check outSupportedValidationTarget
andConstraint
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;</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> max() <span style="color: rgba(0, 0, 255, 1)">default</span> 10<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> isNotNull() <span style="color: rgba(0, 0, 255, 1)">default</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
之后运行时,用反射获取注解,具体不谈。
之前在网上查找这方面技术文章找到的都是这种,给当时的我带来很大困惑。觉得不是我想要的。
参考文章:
http://www.cnblogs.com/liangweiping/p/3837332.html
http://www.cnblogs.com/rixiang/p/5709110.html