我有一个 boolean 字段,我想验证它是否只有“true”或“false”作为值(不带引号)。但是这个字段也允许“true”或“false”作为我想要限制的值(带引号)。
我尝试使用@Pattern 注释来匹配这些值。 但是我有以下错误:
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Pattern' validating type 'java.lang.Boolean'. Check configuration for 'restartable'
我的代码是:
@Pattern(regexp = "^(true|false)$", message = "restartable field allowed input: true or false")
private Boolean restartable;
我该怎么办?还有其他方法可以解决这个问题吗?提前致谢。
最佳答案
如果目标是强制执行非空和真实值。
https://docs.oracle.com/javaee/7/tutorial/bean-validation001.htm
@NotNull
@AssertTrue
private Boolean restartable;
如果值可以为 true 或 false,则 Not Null 注释就足够了,因为 Java 中的 boolean 值只能有两个值。
关于java - 如何在 Spring Boot 中验证类型 'java.lang.Boolean',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56381218/