Java自定义注解进行字段格式校验

通常字段校验使用Hibernate Validator进行校验,如@NotBlank @NotNull @NotEmpty等,但如时间等特殊格式无法校验。但Java提供@Constraint及ConstraintValidator类方便我们自己定义特殊格式字段校验,例如定义不同时间格式校验:

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;


@Target({ElementType.FIELD, ElementType.PARAMETER,ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {DateTime.DateTimeValidator.class})
public @interface DateTime {

    String message() default "542036#时间格式错误,正确格式yyyy-MM-dd HH:mm:ss";

    String format() default "yyyy-MM-dd HH:mm:ss";

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

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

    class DateTimeValidator implements ConstraintValidator<DateTime, String> {

        private String format;

        @Override
        public void initialize(DateTime dateTime) {
            format = dateTime.format();
        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
            
            if (value == null) {
                return true;
            }
            if (value.length() != format.length()) {
                return false;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            try {
                simpleDateFormat.parse(value);
            } catch (ParseException e) {
                return false;
            }
            return true;
        }
    }
}

你可能感兴趣的:(java,java)