我正在使用 spring 数据验证,我想验证Set
或List
但Date
我之前看到的所有示例都在验证单个值,这里是请求类表示
CircuitJurisdictionRequest.java
package eg.intercom.ppo.revamp.model.to.request;
import com.fasterxml.jackson.annotation.JsonFormat;
import eg.intercom.ppo.revamp.model.enums.AppliedLawEnum;
import eg.intercom.ppo.revamp.model.enums.TimeStatusEnum;
import eg.intercom.ppo.revamp.model.to.RequestActivationStatus;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.util.Date;
import java.util.Set;
@Data
@NoArgsConstructor
public class CircuitJurisdictionRequest {
@NotNull(message = "Circuit ID is required")
@Positive(message = "Circuit ID Must Be >= 1")
private Long circuitId;
@NotNull(message = "Table type ID is required")
@Positive(message = "Table type ID Must Be >= 1")
private Long tableTypeId;
@NotNull(message = "Court room ID is required")
@Positive(message = "Court room ID Must Be >= 1")
private Long courtRoomId;
@NotNull(message = "Applied law is required")
private AppliedLawEnum appliedLaw;
@NotNull(message = "Time status is required")
private TimeStatusEnum timeStatus;
@Size(max = 10, message = "Session day should not exceed 10 characters")
@Pattern(
regexp = "^(SUNDAY|MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY)$",
message = "Session day must be a valid day of the week (e.g., SUNDAY, MONDAY, etc.)"
)
private String sessionDay;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Set<@NotNull(message = "Each date in Session Scheduler must not be null") Date> sessionScheduler;
@NotNull(message = "new Cases Capacity is required")
@Min(value = 0, message = "New cases capacity must be zero or positive")
@Max(value = 1000, message = "New cases capacity should not exceed 1000")
private Integer newCasesCapacity;
@NotNull(message = "postponed Cases Capacity is required")
@Min(value = 0, message = "Postponed cases capacity must be zero or positive")
@Max(value = 1000, message = "Postponed cases capacity should not exceed 1000")
private Integer postponedCasesCapacity;
@NotNull(message = "first Session Date is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private LocalDate firstSessionDate; // Optionally add @Future or @NotNull if required
@NotEmpty(message = "At least one digital Jurisdiction is required")
private Set<@Min(value = 0, message = "Digital Jurisdiction Must be >= 0")
@Max(value = 9, message = "Digital Jurisdiction Must be <= 9") Integer> digitalJurisdiction;
private RequestActivationStatus activationStatus;
}
我想要什么
我想验证所有日期sessionScheduler
以 JSON 字符串形式发送到请求中,格式如下dd-MM-yyyy
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Set<@NotNull(message = "Each date in Session Scheduler must not be null") Date> sessionScheduler;
要在 Spring Boot 中验证具有特定格式 () 的日期集合
dd-MM-yyyy
,您需要结合@JsonFormat
日期deserialization
和custom validator
以确保每个日期都符合所需的格式1)定义ValidDateFormat注解进行验证
2)定义DateFormatValidator
3)应用
Validator
到sessionScheduler