本文介绍了在Hibernate Validator 4.1+中,@NotNull,@NotEmpty和@NotBlank有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我似乎无法找到能够区分这三个注释区别的摘要。 解决方案 @NotNull :CharSequence,Collection,Map或Array对象不为空,但可以为空。 @NotEmpty :CharSequence,Collection,Map或Array对象不为null 和size> 0 。 b @NotBlank :字符串不为空,并且修剪后的长度大于零。 为了帮助您理解,我们来看看如何定义和执行这些约束条件(我使用的是版本4.1): @NotNull 约束定义为: @Constraint(validatedBy = {NotNullValidator.class}) 这个类有一个 isValid 方法定义为: public boolean isValid Object object,ConstraintValidatorContext constraintValidatorContext){ return object!= null; @NotEmpty @NotNull @Size(min = 1) 所以这个约束使用 @NotNull 上面的约束,和 @Size 其定义根据对象而不同,但应该是自我解释。 最后, @NotBlank 约束被定义为: @NotNull @Constraint(validatedBy = {NotBlankValidator.class}) 所以这个约束也使用了 @NotNull 约束,但是也受到NotBlankValidator类的限制。这个类有一个定义如下的 isValid 方法: $ b if(charSequence = = null){//好奇返回true; } return charSequence.toString()。trim()。length()> 0; 有趣的是,如果字符串为空,则此方法返回true,但当且仅当长度为修剪后的字符串为0.如果它为空,它可以返回true,因为正如我所提到的, @NotEmpty 定义也需要 @NotNull 。 以下是一些示例: String name = null; @NotNull :false @NotEmpty :false @NotBlank :false String name =; @NotNull : true @NotEmpty :false @NotBlank :false String name =; @NotNull : true @NotEmpty : true @NotBlank :false String name =很好的回答!; @NotNull : true @NotEmpty : true @NotBlank : true I can't seem to be able to find a summary that distinguishes the difference between these three annotations. 解决方案 @NotNull: The CharSequence, Collection, Map or Array object is not null, but can be empty.@NotEmpty: The CharSequence, Collection, Map or Array object is not null and size > 0.@NotBlank: The string is not null and the trimmed length is greater than zero. To help you understand, let's look into how these constraints are defined and carried out (I'm using version 4.1):The @NotNull constraint is defined as: @Constraint(validatedBy = {NotNullValidator.class}) This class has an isValid method defined as: public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) { return object != null; }The @NotEmpty constraint is defined as: @NotNull @Size(min = 1) So this constraint uses the @NotNull constraint above, and @Size whose definition differs based on the object but should be self explanitory. Finally, the @NotBlank constraint is defined as: @NotNull @Constraint(validatedBy = {NotBlankValidator.class}) So this constraint also uses the @NotNull constraint, but also constrains with the NotBlankValidator class. This class has an isValid method defined as: if ( charSequence == null ) { //curious return true; } return charSequence.toString().trim().length() > 0; Interestingly, this method returns true if the string is null, but false if and only if the length of the trimmed string is 0. It's ok that it returns true if it's null because, as I mentioned, the @NotEmpty definition also requires @NotNull. Here are a few examples: String name = null; @NotNull: false @NotEmpty: false @NotBlank: false String name = ""; @NotNull: true @NotEmpty: false @NotBlank: false String name = " "; @NotNull: true @NotEmpty: true @NotBlank: false String name = "Great answer!"; @NotNull: true @NotEmpty: true @NotBlank: true 这篇关于在Hibernate Validator 4.1+中,@NotNull,@NotEmpty和@NotBlank有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 03:22