gpt4 book ai didi

java - Javax 和 Hibernate 验证是否暗示 NotNull?

转载 作者:行者123 更新时间:2023-11-29 05:13:54 24 4
gpt4 key购买 nike

支持的 Javax 验证列表是 here .

支持的 Hibernate 验证列表(扩展 Javax)是 here .

这些注释中的每一个都是“扩展”或暗示 @NotNull 吗?

例如,如果我注释以下实体:

public class Widget {
@Email
private String email;

@URL
private String website;

// etc...
}

@Email@URL 是否也强制执行 @NotNull?或者,如果这些属性是为特定的 Widget 实例定义的, 它们只是应用/强制执行?

哪些注释扩展/暗示 @NotNull,哪些不扩展/暗示?这些注释是否有任何其他隐含关系?

最佳答案

没有。如果您查看 source codeEmailValidator例如,来自 hibernate validator ,它在 isValid() 方法的开头包含此代码:

if ( value == null || value.length() == 0 ) {
return true;
}

同样适用于 URLValidator 等等。通常,当您尝试验证 null 值时,相应注释的所有 validator 都认为该值有效,因此一个好的经验法则是始终单独执行非空验证。

编辑:这里引用自JSR-303 specification与此问题相关:

While not mandatory, it is considered a good practice to split the core constraint validation from the not null constraint validation (for example, an @Email constraint will return true on a null object, i.e. will not take care of the @NotNull validation)

关于java - Javax 和 Hibernate 验证是否暗示 NotNull?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27257885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com