Null 但既不是 Empty 也不是 Blank 的 Java 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31132477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java annotation for Null but neither Empty nor Blank
提问by Yash Krishnan
Is there are any java annotation(s) that can validate like the example below ?
是否有任何 Java 注释可以像下面的示例一样进行验证?
String test;
test = null; //valid
test = ""; //invalid
test = " "; //invalid
test = "Some values"; //valid
Thanks in advance
提前致谢
回答by bhdrk
Did you try Hibernate-Validator? I think that's what you are looking for.
你试过Hibernate-Validator吗?我想这就是你要找的。
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
public class MyModel {
@NotNull
private String str1;
@NotEmpty
private String str2;
@NotBlank
private String str3;
}
回答by Jules
There isn't such an annotation in either javax.validation or Hibernate Validator. There was a request to add one to Hibernate Validatorbut it was closed as "won't fix" due to the possibility of writing your own relatively easily. The suggest solution was to either use your own annotation type defined like this:
javax.validation 或 Hibernate Validator 中都没有这样的注释。有人请求向 Hibernate Validator 添加一个,但由于可能相对容易地编写自己的验证器,因此它被关闭为“无法修复”。建议的解决方案是使用您自己定义的注释类型,如下所示:
@ConstraintComposition(OR)
@Null
@NotBlank
@ReportAsSingleViolation
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface NullOrNotBlank {
String message() default "{org.hibernate.validator.constraints.NullOrNotBlank.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
or to use the @Pattern
annotation with a regular expression that requires a non-whitespace character to be present (as the Pattern annotation accepts nulls and does not match them against the pattern).
或者将@Pattern
注释与需要存在非空白字符的正则表达式一起使用(因为 Pattern 注释接受空值并且不将它们与模式匹配)。
回答by vegemite4me
You need to create a custom annotation: @NullOrNotBlank
您需要创建自定义注释: @NullOrNotBlank
First create the custom annotation: NullOrNotBlank.java
首先创建自定义注解: NullOrNotBlank.java
@Target( {ElementType.FIELD})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = NullOrNotBlankValidator.class)
public @interface NullOrNotBlank {
String message() default "{javax.validation.constraints.Pattern.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default {};
}
Then the actual validator: NullOrNotBlankValidator.java
然后是实际的验证器: NullOrNotBlankValidator.java
public class NullOrNotBlankValidator implements ConstraintValidator<NullOrNotBlank, String> {
public void initialize(NullOrNotBlank parameters) {
// Nothing to do here
}
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
return str == null || str.trim().length() > 0;
}
}