简体   繁体   English

java LocalDate 验证消息和 Junit

[英]java LocalDate validation message and Junit

I have below property in POJO -我在 POJO 中有以下财产 -

@NotNull(message = "dateOfBirth is required")
@DateFormat(format = "YYYY-MM-DD", message = "dateOfBirth should be in format YYYY-MM-DD")
@JsonDeserialize(using = LocalDateDeserializer.class)
LocalDate dateOfBirth;

For custom message of validation I have added below validator -对于自定义验证消息,我在验证器下方添加了 -

@Documented
@Constraint(validatedBy = DateFormatValidator.class)
@Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
public @interface DateFormat {
   String format();
   String message() default "Invalid date format";
   Class<?>[] groups() default {};
   Class<? extends Payload>[] payload() default {};
}

public class DateFormatValidator implements ConstraintValidator<DateFormat, LocalDate> {

private String dateFormat;

@Override
public void initialize(DateFormat constraintAnnotation){
    this.dateFormat = constraintAnnotation.format();
}

@Override
public boolean isValid(LocalDate value, ConstraintValidatorContext context) {
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    try{
        sdf.setLenient(false);
        Date d = sdf.parse(String.valueOf(value));
        return true;
    }catch(ParseException e) {
        return false;
    }
}

} }

Now for my junit I am adding -现在对于我的junit,我要添加-

.dateOfBirth(LocalDate.of(1984, 03, 12))

but when I run my junit, I am getting my validator message means dateOfBirth should be in format YYYY-MM-DD .但是当我运行我的 junit 时,我收到我的验证器消息意味着dateOfBirth 应该采用 YYYY-MM-DD 格式 How can I pass date into my junit to satisfy above condition and should run junit.如何将日期传递到我的 junit 以满足上述条件并应该运行 junit。

This doesn't make sense for several reasons.由于多种原因,这没有意义。

First, because YYYY-MM-DD means "week year", then month, then "day in year".首先,因为 YYYY-MM-DD 的意思是“周年”,然后是月,然后是“年中的日”。 Nobody will ever enter a date in that format.没有人会以这种格式输入日期。 You probably mean yyyy-MM-dd.您可能是指 yyyy-MM-dd。

Second, because using the old, obsolete SimpleDateFormat and Date classes instead of the new, well-thought java.time package classes to parse dates is really a strange idea.其次,因为使用旧的、过时的SimpleDateFormatDate类而不是新的、经过深思熟虑的java.time包类来解析日期确实是一个奇怪的想法。

And most importantly, because a LocalDate doesn't have a format.最重要的是,因为LocalDate没有格式。 So you can't validate that a LocalDate has the right format.因此,您无法验证 LocalDate 是否具有正确的格式。 A LocalDate is not a String. LocalDate 不是字符串。 Whatever format you use to parse a String into a LocalDate, in the end, you end up with a LocalDate, which doesn't remember the format of the original parsed String you used.无论您使用何种格式将 String 解析为 LocalDate,最终都会得到 LocalDate,它不记得您使用的原始解析字符串的格式。 Just like when you have an Integer , you can't possibly know if the user who entered that integer did it in decimal format, or in binary, or in hexadecimal.就像当你有一个Integer ,你不可能知道输入这个整数的用户是用十进制格式,还是二进制格式,还是十六进制格式。 That parsing happened before the Integer was created, and the Integer object only contains the value of the int, not its original format on the JSON structure.该解析发生在创建 Integer 之前,并且 Integer 对象仅包含 int 的值,而不包含其在 JSON 结构上的原始格式。

I think that you are over-thinking your issue.我认为你是在过度考虑你的问题。 You are trying to write stuff that already done for you automatically.您正在尝试编写已经自动为您完成的东西。 First of all you have a wrong format - most likely you mean "yyyy-MM-dd" - See the explanation here: DateTimeFormatter .首先,您的格式有误-很可能您的意思是“yyyy-MM-dd”-请参阅此处的说明: DateTimeFormatter Second - all you will need to do is:其次 - 您需要做的就是:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
public ZonedDateTime getTime() {
    return time;
}

For more details see the answers here and may be here有关更多详细信息,请参阅此处的答案,可能在此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM