1. 커스텀 어노테이션 생성.

2. validate 부분 작성.

3. VO에 적용.

 

1.  커스텀 어노테이션 생성.

@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FromDateBeforeOrSameAsToDateValidator.class)
@Documented
public @interface FromDateBeforeOrSameAsToDate {
    String message() default "시작일은 종료일보다 클수 없습니다";

    Class[] groups() default {};

    Class[] payload() default {};

    String searchFromDt();

    String searchEndDt();

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

 

2. validate 작성.

public class FromDateBeforeOrSameAsToDateValidator implements ConstraintValidator<FromDateBeforeOrSameAsToDate, Object> {

    private String searchFromDt;
    private String searchEndDt;

    @Override
    public void initialize(FromDateBeforeOrSameAsToDate constraintAnnotation) {
        searchFromDt = constraintAnnotation.searchFromDt();
        searchEndDt = constraintAnnotation.searchEndDt();
    }

    @Override
    public boolean isValid(Object requestObject, ConstraintValidatorContext constraintValidatorContext) {
        Field fromDateField = FieldUtils.getField(requestObject.getClass(), searchFromDt, true);
        Field toDateField = FieldUtils.getField(requestObject.getClass(), searchEndDt, true);

        String fromLocalDate = (String) ReflectionUtils.getField(fromDateField, requestObject);
        String toLocalDate = (String) ReflectionUtils.getField(toDateField, requestObject);

        SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMdd");
        transFormat.setLenient(false); //유효한 날짜인지 체크 예를들어 2019/01/32 이런거 체크
        Date from = new Date();
        Date to = new Date();

        if (fromLocalDate == null || toLocalDate == null) {
            return true;
        }

        try {
             from = transFormat.parse(fromLocalDate);
             to = transFormat.parse(toLocalDate);
            if(from.compareTo(to) > 0){ // Date 1 occurs after Date 2
                return false;
            }
        } catch (ParseException e) {
            constraintValidatorContext.buildConstraintViolationWithTemplate("유효하지 않은 날짜 입니다.") .addConstraintViolation();
            return false;
        }

        constraintValidatorContext.disableDefaultConstraintViolation();
        constraintValidatorContext.buildConstraintViolationWithTemplate(
                "시작일은 종료일보다 클수 없습니다.")
                .addConstraintViolation();

        return fromLocalDate.equals(toLocalDate) ||  (from.compareTo(to) < 0);
    }
}

 

3. VO에 적용.

@FromDateBeforeOrSameAsToDate.List({ @FromDateBeforeOrSameAsToDate(searchFromDt = "regStartDate", searchEndDt = "regEndDate" })
public class EmployeeContractDelivery extends BaseObject {

        /*등록시작일자*/
        private String regStartDate;
        /*등록종료일자*/
        private String regEndDate;

 

        public String getRegStartDate() {
            return regStartDate;
        }  
        public void setRegStartDate(String regStartDate) {
            this.regStartDate = regStartDate;
        }
        public String getRegEndDate() {
            return regEndDate;
        }
        public void setRegEndDate(String regEndDate) {
            this.regEndDate = regEndDate;
        }

}

'Web개발 > Java' 카테고리의 다른 글

Url PathVariable 변수명 가져오기  (0) 2020.03.05
@SpringBootTest 와 @WebMvcTest 차이  (0) 2019.07.07

+ Recent posts