Springboot 自定义validation实现开始时间不能大于结束时间

代码

package com.peterpyx.myutilpro.util.validation;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;
import java.util.Date;

/**
 * @author cr 2020-04-21 校验结束时间大于等于开始时间
 */
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE,
        ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = CheckTimeInterval.CheckTimeIntervalValidation.class)
@Documented
@Repeatable(CheckTimeInterval.List.class)
public @interface CheckTimeInterval {

    String[] beginTime() default {"beginTime"};

    String[] endTime() default {"endTime"};

    String message() default "开始时间不能大于结束时间 ";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,
            ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface List {
        CheckTimeInterval[] value();
    }


    class CheckTimeIntervalValidation implements ConstraintValidator<CheckTimeInterval, Object> {

        private String[] beginTime;
        private String[] endTime;

        @Override
        public void initialize(CheckTimeInterval constraintAnnotation) {
            this.beginTime = constraintAnnotation.beginTime();
            this.endTime = constraintAnnotation.endTime();
        }

        @Override
        public boolean isValid(Object value, ConstraintValidatorContext context) {
            BeanWrapper beanWrapper = new BeanWrapperImpl(value);
            boolean valid = true;
            for (int i = 0; i < beginTime.length; i++) {
                String s = beginTime[i];
                Object propertyValue = beanWrapper.getPropertyValue(s);
                Object propertyValue1 = beanWrapper.getPropertyValue(endTime[i]);
                if (ObjectUtils.isNotEmpty(propertyValue) && ObjectUtils.isNotEmpty(propertyValue1)) {
                    Date beginTimeVal = (Date) propertyValue;
                    Date endTimeVal = (Date) propertyValue1;
                    int result = endTimeVal.compareTo(beginTimeVal);
                    if (result < 0) {
                        valid = false;
                        break;
                    }
                }
            }
            return valid;
        }
    }
}

使用:

@Data
@CheckTimeInterval(beginTime = {"beginTime","start"},endTime = {"endTime","end"})
public class TEST{

    @ApiModelProperty("身份证号码")
    @NotBlank(message = "身份证号码不为空")
    private String idcard;

    private Date beginTime;
    private Date endTime;


    private Date start;
    private Date end;

}

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值