You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中如何验证特定格式的日期集合

验证Set集合中日期的格式(dd-MM-yyyy)

你当前的代码里,@JsonFormat仅用于指定Jackson序列化/反序列化日期的格式,不具备格式验证能力。如果传入的日期字符串格式不符合要求,Jackson会直接抛出反序列化异常,而非通过Spring Validation返回友好的错误提示。你已经用@NotNull确保集合元素不为空,但还需要验证每个日期必须严格遵循dd-MM-yyyy格式,以下是具体解决方案:

方案一:自定义验证注解(适配Date类型)

1. 创建自定义验证注解

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = DateFormatValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidDateFormat {
    String message() default "日期格式无效,必须为dd-MM-yyyy";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    String pattern() default "dd-MM-yyyy";
}

2. 实现验证器逻辑

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatValidator implements ConstraintValidator<ValidDateFormat, Date> {
    private String pattern;

    @Override
    public void initialize(ValidDateFormat constraintAnnotation) {
        this.pattern = constraintAnnotation.pattern();
    }

    @Override
    public boolean isValid(Date date, ConstraintValidatorContext context) {
        if (date == null) {
            return true; // 空值交给@NotNull处理
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            sdf.setLenient(false); // 开启严格模式,拒绝格式不匹配的非法日期
            // 验证日期能否被解析并还原为原日期,避免"32-13-2024"这类无效日期被转换
            String formattedDate = sdf.format(date);
            Date parsedDate = sdf.parse(formattedDate);
            return date.equals(parsedDate);
        } catch (Exception e) {
            return false;
        }
    }
}

3. 改造请求类字段

将自定义注解添加到集合元素上:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@NotEmpty(message = "Session Scheduler不能为空") // 可选,根据业务需求添加
private Set<@NotNull(message = "Session Scheduler中的每个日期都不能为null") 
           @ValidDateFormat(pattern = "dd-MM-yyyy") 
           Date> sessionScheduler;

方案二:改用LocalDate(推荐,Java 8+日期API)

Date属于旧版API,推荐使用java.time.LocalDate,它更安全且功能更完善。可以结合@Pattern先验证字符串格式,再通过Jackson自动转换为LocalDate:

修改字段定义

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@NotEmpty(message = "Session Scheduler不能为空")
private Set<@NotNull(message = "Session Scheduler中的每个日期都不能为null") 
           @Pattern(regexp = "^\\d{2}-\\d{2}-\\d{4}$", message = "日期格式必须为dd-MM-yyyy")
           LocalDate> sessionScheduler;

注意事项

如果直接使用LocalDate,Jackson会自动将字符串转为日期对象,若格式错误会抛出反序列化异常。若想在Spring Validation阶段返回格式错误提示,可以先将集合元素定义为String,验证格式后再手动转换为LocalDate,或者为LocalDate编写自定义验证器。

确保验证生效

在Controller的请求参数上添加@Valid或@Validated注解,触发Spring Validation:

@PostMapping("/circuit-jurisdiction")
public ResponseEntity<?> createCircuitJurisdiction(
    @Valid @RequestBody CircuitJurisdictionRequest request
) {
    // 业务逻辑处理
}

内容的提问来源于stack exchange,提问作者Youssef Gamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 08:03:15