springboot学习记录之使用校验@Validated Date时间类型校验出现String不能转换Date

本文介绍如何解决Spring框架中因日期格式不匹配导致的转换异常问题,通过自定义StringToDateConverter转换器,实现对字符串到日期类型转换的灵活处理,避免了因前端传入不规范时间格式而导致的程序错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当控制器的对象通过@Validated注解标注date类型的字段时候,前端在传入123等不符合时间类型的操作时候会出现

Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createdTime1'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '1234'; nested exception is java.lang.IllegalArgumentException

这是因为spring在处理转换的时候是捕获异常直接抛出的

如图

所以做一下处理注册一个转换器

import com.sizheng.SuzhengApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 自定义spring校验的时间格式转换,如果转换不通过将不做报错处理
 */
public class StringToDateConverter extends PropertyEditorSupport {

    public Date convert(String value)  {
        if(StringUtils.isEmpty(value)) {
            return null;
        }
        value = value.trim();
        Date dtDate=null;
        try {
            if( Pattern.matches("^-?\\d+$", value)){
                if(value.length()>11){
                    dtDate=new Date( Long.valueOf(value));
                }else if(value.length()==11){
                    dtDate=new Date( Long.valueOf(value)*1000);
                }
            }else{
                dtDate = dateFormat.parse(value);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            dtDate=null;
        }
        return dtDate;
    }
    private final DateFormat dateFormat;
    private final boolean allowEmpty;
    private final int exactDateLength;

    public StringToDateConverter(DateFormat dateFormat, boolean allowEmpty) {
        this.dateFormat = dateFormat;
        this.allowEmpty = allowEmpty;
        this.exactDateLength = -1;
    }

    public StringToDateConverter(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) {
        this.dateFormat = dateFormat;
        this.allowEmpty = allowEmpty;
        this.exactDateLength = exactDateLength;
    }

    public void setAsText(@Nullable String text) throws IllegalArgumentException {
        if(this.allowEmpty && !StringUtils.hasText(text)) {
            this.setValue((Object)null);
        } else {
            if(text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
                throw new IllegalArgumentException("Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
            }

            this.setValue(convert(text));
        }

    }
    public static void main(String[] args) {
    }
    public String getAsText() {
        Date value = (Date)this.getValue();
        return value != null?this.dateFormat.format(value):"";
    }

}

然后后控制器注册

@InitBinder
protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new StringToDateConverter(dateFormat, false));
}
### 使用 `@Validated` 进行时间格式校验 为了实现更复杂的时间格式校验,通常需要创建自定义的验证注解。下面是一个完整的例子展示如何通过自定义注解来确保传入的服务层方法中的日期字段满足特定的要求。 #### 创建自定义约束注解 首先定义一个新的注解用于标记待检验属性: ```java import javax.validation.Constraint; import javax.validation.Payload; import java.lang.annotation.*; @Documented @Constraint(validatedBy = DateValidator.class) @Target({ ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) public @interface ValidDate { String message() default "Invalid date format or past date"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } ``` 接着编写对应的验证器逻辑: ```java import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.time.LocalDate; public class DateValidator implements ConstraintValidator<ValidDate, LocalDate> { private static final int DAYS_IN_FUTURE_REQUIRED = 10; @Override public void initialize(ValidDate constraintAnnotation) {} @Override public boolean isValid(LocalDate value, ConstraintValidatorContext context){ if (value == null || !isFuture(value)) { return false; } long daysBetween = ChronoUnit.DAYS.between(LocalDate.now(), value); return daysBetween >= DAYS_IN_FUTURE_REQUIRED; } private boolean isFuture(LocalDate localDate){ return localDate.isAfter(LocalDate.now()); } } ``` 最后,在实体类中应用此注解并结合 `@Validated` 来触发校验过程: ```java import org.springframework.validation.annotation.Validated; @Validated public class EventRequest { @ValidDate private LocalDate eventDate; // getters and setters... } ``` 当接收到请求时,如果提供的日期不符合设定的标准,则会抛出异常阻止进一步处理,并返回错误信息给客户端[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值