[英]javax.validation.constraints for primitive boolean?
Small question on javax.validation.constraints and how to validate the primitive boolean (not Boolean)关于 javax.validation.constraints 的小问题以及如何验证原始布尔值(不是布尔值)
Currently, I am using @Min(1), @Size(min = 1, max = 10), @NotBlank, etc, to validate my primitive int, and String object.目前,我正在使用 @Min(1)、@Size(min = 1, max = 10)、@NotBlank 等来验证我的原始 int 和 String 对象。 It is doing the job fine, very happy.
它做得很好,很高兴。
How to validate the same for a primitive boolean please?请如何验证原始布尔值的相同内容? Meaning, I have a boolean that I would like to be a must input in the JSON request.
意思是,我有一个布尔值,我想是在JSON请求必须输入。
I understand if I do Boolean, I can combine it wit some kind of @NotNull validation.我明白如果我做布尔值,我可以将它与某种 @NotNull 验证结合起来。 How to do the same with the primitive boolean please?
请如何对原始布尔值做同样的事情?
I would like the client to must input this field.我希望客户必须输入此字段。 And the value of the field must be either true or false.
并且该字段的值必须为真或假。
I do not want to default it to false or true if the field is not present.如果该字段不存在,我不想将其默认为 false 或 true。
This is a valid example这是一个有效的例子
{
"field": false
}
Here, there is a typo in the field, hence, the field field is not present, it is not valid.此处,字段中存在拼写错误,因此字段字段不存在,无效。
{
"fielddwdw": false
}
Here, the value of the field is "true", the string, hence invalid.此处,该字段的值为“true”,即字符串,因此无效。
{
"field": "true"
}
Here, the value is neither true or false, invalid这里的值不是真也不是假,无效
{
"field": foo
}
I was hoping for some kind of我希望有某种
@MagicalBooleanValidationAnnoation
private boolean field;
But just would like to achieve this use case.但只是想实现这个用例。
Thank you谢谢
You can't do the same as a Boolean
with @NotNull
annotation for a primitive boolean
, because the primitive boolean
has only 2 possible values, and none of them represent the lack of a value.对于原始
boolean
,您不能使用带有@NotNull
注释的Boolean
做同样的事情,因为原始boolean
只有 2 个可能的值,并且没有一个表示缺少值。
If you want a 3-state value, with states true
, false
, and missing
, you must use Boolean
.如果您想要一个三态值,状态为
true
、 false
和missing
,则必须使用Boolean
。
Or you can use an enum
with 3 values.或者您可以使用具有 3 个值的
enum
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.