• Spring Boot:自定义注解--annotation


    自定义注解的定义和作用范围

    • 自定义注解可以作用在类、方法、属性、参数、异常、字段或其他注解上。

    如何创建自定义注解

    创建注解接口

    在这里插入图片描述

    package hanshuhuan.test.anonotion;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * 实体检验自定义注解类,根据我们自定义的注解去检查实体各个字段是否在规定的值内
     * @author shuhu
     *
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ValidateEntity {
    	
    	public boolean required() default false;//是否检验null
    	
    	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 "";//值为null时的错误提示信息
    	
    	public String errorMinLengthMsg() default "";//最小长度不满足时的提示信息
    	
    	public String errorMaxLengthMsg() default "";//最大长度不满足时的提示信息
    	
    	public String errorMinValueMsg() default "";//最小值不满足时的提示信息
    	
    	public String errorMaxValueMsg() default "";//最大值不满足时的提示信息
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    如何使用自定义注解进行数据验证

    创建注解处理器

    package hanshuhuan.test.util;
    
    import java.lang.reflect.Field;
    
    import hanshuhuan.test.anonotion.ValidateEntity;
    import hanshuhuan.test.bean.CodeMsg;
    
    /**
     * 验证实体工具类
     * @author shuhu
     *
     */
    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;
    						}
    						//到这,说明该变量的值不是null
    						//首先判断是不是String类型
    						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) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					} catch (IllegalAccessException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			}
    		}
    		return CodeMsg.SUCCESS;
    	}
    	
    	/**
    	 * 检查对象是否是数字类型
    	 * @param object
    	 * @return
    	 */
    	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;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    控制器中使用注解

    @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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    如何为字段添加注解

    • 自定义注解的定义是通过@注解名的方式实现的。
    @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;//密码
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    编写覆盖全面的测试用例
    GBase 8c V3.0.0数据类型——账本数据库的函数
    如何关联分段代码表生成统计报表
    APP每次打开都需要登录
    【LeetCode刷题】146. LRU 缓存
    视频美颜sdk代码分析与人脸识别精准度问题
    25-SpringBoot 自定义starter
    基于Android的教室预约系统
    基于Spring Boot应用Apache CXF发布Web Services服务
    html:mate
  • 原文地址:https://blog.csdn.net/weixin_45880844/article/details/133680808