目录 自定义注解的定义和作用范围 如何创建自定义注解 如何使用自定义注解进行数据验证 如何为字段添加注解
自定义注解的定义和作用范围
自定义注解可以作用在类、方法、属性、参数、异常、字段或其他注解上。
如何创建自定义注解
创建注解接口
package hanshuhuan. test. anonotion ; import java. lang. annotation. ElementType ;
import java. lang. annotation. Retention ;
import java. lang. annotation. RetentionPolicy ;
import java. lang. annotation. Target ;
@Target ( ElementType . FIELD )
@Retention ( RetentionPolicy . RUNTIME )
public @interface ValidateEntity { public boolean required ( ) default false ; public boolean requiredLeng ( ) default false ; public boolean requiredMaxValue ( ) default false ; public boolean requiredMinValue ( ) default false ; public int maxLength ( ) default - 1 ; public int minLength ( ) default - 1 ; public long maxValue ( ) default - 1 ; public long minValue ( ) default - 1 ; public String errorRequiredMsg ( ) default "" ; public String errorMinLengthMsg ( ) default "" ; public String errorMaxLengthMsg ( ) default "" ; public String errorMinValueMsg ( ) default "" ; public String errorMaxValueMsg ( ) default "" ;
}
如何使用自定义注解进行数据验证
创建注解处理器
package hanshuhuan. test. util ; import java. lang. reflect. Field ; import hanshuhuan. test. anonotion. ValidateEntity ;
import hanshuhuan. test. bean. CodeMsg ;
public class ValidateEntityUtil { public static CodeMsg validate ( Object object) { Field [ ] declaredFields = object. getClass ( ) . getDeclaredFields ( ) ; for ( Field field : declaredFields) { ValidateEntity annotation = field. getAnnotation ( ValidateEntity . class ) ; if ( annotation != null ) { if ( annotation. required ( ) ) { field. setAccessible ( true ) ; try { Object o = field. get ( object) ; if ( o == null ) { CodeMsg codeMsg = CodeMsg . VALIDATE_ENTITY_ERROR ; codeMsg. setMsg ( annotation. errorRequiredMsg ( ) ) ; return codeMsg; } if ( o instanceof String ) { if ( annotation. requiredLeng ( ) ) { if ( o. toString ( ) . length ( ) < annotation. minLength ( ) ) { CodeMsg codeMsg = CodeMsg . VALIDATE_ENTITY_ERROR ; codeMsg. setMsg ( annotation. errorMinLengthMsg ( ) ) ; return codeMsg; } if ( o. toString ( ) . length ( ) > annotation. maxLength ( ) ) { CodeMsg codeMsg = CodeMsg . VALIDATE_ENTITY_ERROR ; codeMsg. setMsg ( annotation. errorMaxLengthMsg ( ) ) ; return codeMsg; } } } if ( isNumberObject ( o) ) { if ( annotation. requiredMinValue ( ) ) { if ( Double . valueOf ( o. toString ( ) ) < annotation. minValue ( ) ) { CodeMsg codeMsg = CodeMsg . VALIDATE_ENTITY_ERROR ; codeMsg. setMsg ( annotation. errorMinValueMsg ( ) ) ; return codeMsg; } } if ( annotation. requiredMaxValue ( ) ) { if ( Double . valueOf ( o. toString ( ) ) > annotation. maxValue ( ) ) { CodeMsg codeMsg = CodeMsg . VALIDATE_ENTITY_ERROR ; codeMsg. setMsg ( annotation. errorMaxValueMsg ( ) ) ; return codeMsg; } } } } catch ( IllegalArgumentException e) { e. printStackTrace ( ) ; } catch ( IllegalAccessException e) { e. printStackTrace ( ) ; } } } } return CodeMsg . SUCCESS ; } public static boolean isNumberObject ( Object object) { if ( object instanceof Integer ) return true ; if ( object instanceof Long ) return true ; if ( object instanceof Float ) return true ; if ( object instanceof Double ) return true ; return false ; }
}
控制器中使用注解
@RequestMapping ( value= "/login" , method= RequestMethod . POST )
@ResponseBody
public Result < Boolean > login ( User user, String cpacha) { if ( user == null ) { return Result . error ( CodeMsg . DATA_ERROR ) ; } CodeMsg validate = ValidateEntityUtil . validate ( user) ; if ( validate. getCode ( ) != CodeMsg . SUCCESS . getCode ( ) ) { return Result . error ( validate) ; } if ( StringUtils . isEmpty ( cpacha) ) { return Result . error ( CodeMsg . CPACHA_EMPTY ) ; } log. info ( "ok" + user) ; return Result . success ( true ) ;
}
如何为字段添加注解
@ValidateEntity ( required= true , requiredLeng= true , minLength= 4 , maxLength= 18 , errorRequiredMsg= "用户名不能为空!" , errorMinLengthMsg= "用户名长度需大于4!" , errorMaxLengthMsg= "用户名长度不能大于18!" )
@Column ( name= "username" , nullable= false , length= 18 )
private String username; @ValidateEntity ( required= true , requiredLeng= true , minLength= 4 , maxLength= 32 , errorRequiredMsg= "密码不能为空!" , errorMinLengthMsg= "密码长度需大于4!" , errorMaxLengthMsg= "密码长度不能大于32!" )
@Column ( name= "password" , nullable= false , length= 32 )
private String password;