java/beans 验证 - collection/map 不包含空值
java/beans validation - collection/map does not contain nulls
有 @NotNull
注释验证某个对象不为 null。
有 @NotEmpty
注释验证某个 collection/map/string/... 不为空。
是否还有一个注释可以验证某个 collection/map 不包含任何空值?我找不到它。它看起来很基础,我相信它必须在 JSR-303 规范中。
我遇到了同样的问题。我找到的唯一解决方案是,将空验证添加到实体的设置器中。如果提交的值为null -> return。我知道那很丑陋,但我知道如何避免异常的唯一方法。
没有这样的内置约束。您可以轻松编写自定义约束,例如@NoNullElements,它可以满足您的需求。请参阅参考文档 http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints 了解如何编写自定义约束。
Bean 验证 2.0/Hibernate 验证器 6.0
Bean Validation 2.0(其中 Hibernate Validator 6.0 是参考实现)允许直接在泛型类型参数上使用其验证注释。这在 Hibernate 6.0 release documentation:
中注明
Hibernate Validator 6.0 is the Reference Implementation of the Bean Validation 2.0 specification so it comes with all its new features:
- First class support of container element constraints and cascaded validation (think
private Map<@Valid @NotNull OrderCategory, List<@Valid @NotNull Order>> orderByCategories;
);
如果项目使用 Java 8 with Bean Validation 2.0,此功能可用于实现您规定的目标:
List<@NotNull String> noNullsList;
Map<@NotNull String, @NotNull String> noNullKeysOrValuesMap;
Bean 验证 1.2/Hibernate 验证器 5.2
Hibernate 5.2(带有 Bean Validation 1.2)添加了该功能的有限版本以允许直接在泛型类型参数上进行验证注释。但是,可以以这种方式使用其内置的 Bean 验证或 Hibernate 验证约束的 none,因为出于向后兼容的原因,注释未指定 ElementType.TYPE_USE
。此外,可以为映射值而不是映射键指定类型参数约束。这些都在 Hibernate Validator 5.2 documentation:
中描述
Starting from Java 8, it is possible to specify constraints directly on the type argument of a parameterized type. However, this requires that ElementType.TYPE_USE
is specified via @Target
in the constraint definition. To maintain backwards compatibility, built-in Bean Validation as well as Hibernate Validator specific constraints do not yet specify ElementType.TYPE_USE
.
[...]
When applying constraints on an Iterable type argument, Hibernate Validator will validate each element.
[...]
Type argument constraints are also validated for map values. Constraints on the key are ignored.
总结
总而言之,如果您将 Java 8 与 Bean Validation 2.0(例如 Hibernate Validator 6)一起使用,则可以使用 @NotNull
.[=23 注释通用列表和映射类型参数=]
如果您将 Java 8 与 Bean Validation 1.2 和 Hibernate Validator 5.2 一起使用,您可以编写自定义 @NotNull
验证注解,在其定义中包含 TYPE_USE
,并将其应用于集合或映射值的通用类型。
如果您使用的不是 Java 8 或 5.2 之前的 Hibernate Validator 版本,则需要将自定义约束应用于映射或列表,以验证集合中的每个元素或地图是非空的。
有 @NotNull
注释验证某个对象不为 null。
有 @NotEmpty
注释验证某个 collection/map/string/... 不为空。
是否还有一个注释可以验证某个 collection/map 不包含任何空值?我找不到它。它看起来很基础,我相信它必须在 JSR-303 规范中。
我遇到了同样的问题。我找到的唯一解决方案是,将空验证添加到实体的设置器中。如果提交的值为null -> return。我知道那很丑陋,但我知道如何避免异常的唯一方法。
没有这样的内置约束。您可以轻松编写自定义约束,例如@NoNullElements,它可以满足您的需求。请参阅参考文档 http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints 了解如何编写自定义约束。
Bean 验证 2.0/Hibernate 验证器 6.0
Bean Validation 2.0(其中 Hibernate Validator 6.0 是参考实现)允许直接在泛型类型参数上使用其验证注释。这在 Hibernate 6.0 release documentation:
中注明Hibernate Validator 6.0 is the Reference Implementation of the Bean Validation 2.0 specification so it comes with all its new features:
- First class support of container element constraints and cascaded validation (think
private Map<@Valid @NotNull OrderCategory, List<@Valid @NotNull Order>> orderByCategories;
);
如果项目使用 Java 8 with Bean Validation 2.0,此功能可用于实现您规定的目标:
List<@NotNull String> noNullsList;
Map<@NotNull String, @NotNull String> noNullKeysOrValuesMap;
Bean 验证 1.2/Hibernate 验证器 5.2
Hibernate 5.2(带有 Bean Validation 1.2)添加了该功能的有限版本以允许直接在泛型类型参数上进行验证注释。但是,可以以这种方式使用其内置的 Bean 验证或 Hibernate 验证约束的 none,因为出于向后兼容的原因,注释未指定 ElementType.TYPE_USE
。此外,可以为映射值而不是映射键指定类型参数约束。这些都在 Hibernate Validator 5.2 documentation:
Starting from Java 8, it is possible to specify constraints directly on the type argument of a parameterized type. However, this requires that
ElementType.TYPE_USE
is specified via@Target
in the constraint definition. To maintain backwards compatibility, built-in Bean Validation as well as Hibernate Validator specific constraints do not yet specifyElementType.TYPE_USE
.[...]
When applying constraints on an Iterable type argument, Hibernate Validator will validate each element.
[...]
Type argument constraints are also validated for map values. Constraints on the key are ignored.
总结
总而言之,如果您将 Java 8 与 Bean Validation 2.0(例如 Hibernate Validator 6)一起使用,则可以使用 @NotNull
.[=23 注释通用列表和映射类型参数=]
如果您将 Java 8 与 Bean Validation 1.2 和 Hibernate Validator 5.2 一起使用,您可以编写自定义 @NotNull
验证注解,在其定义中包含 TYPE_USE
,并将其应用于集合或映射值的通用类型。
如果您使用的不是 Java 8 或 5.2 之前的 Hibernate Validator 版本,则需要将自定义约束应用于映射或列表,以验证集合中的每个元素或地图是非空的。