javax.validation
I have a Java EE application and I want to validate a Date. With a String I do this:
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
...
@NotNull
@Size(min = 1, max = 255)
private String myString;
But now I have two dates which I want to validate. The user can in the frontend system write a String in a text field which will be transferred via JSON (I have to use text field, I can not use a datepicker).
So my backend does have this in my domain class:
@DateTimeFormat(pattern = "dd.MM.yy")
@Temporal(value=TemporalType.TIMESTAMP)
private Date myStartDate;
@DateTimeFormat(pattern = "dd.MM.yy")
@Temporal(value=TemporalType.TIMESTAMP)
private Date myEndDate;
I want to validate against the format "dd.MM.yyyy". How can this be done?
And, I do not think so, but is there an automatic validation to check if the start date is before the end date? I only found @Future
and @Past
.
So the only solution is to use a @Pattern, a regular expression?!
Thank you in advance for your help, Best Regards.
@DateTimeFormat is used during web data binding, when mapping request parameters onto an object (assuming you have enabled it with <mvc:annotation-driven/>
or manually.) It's not generally going to be used when deserializing JSON into an object. How are you reading in your JSON? What are you using to deserialize it? You can't validate a java Date object after the fact for the formatting, you have to check up front before deserialization.
There are no multi-field constraints built in. You'll want to write your own type level constraint if you want to compare two properties on an object.
链接地址: http://www.djcxy.com/p/4324.html上一篇: 重写Django的RelatedManager方法
下一篇: javax.validation