gpt4 book ai didi

spring - SmartValidator - 使用组手动调用验证

转载 作者:行者123 更新时间:2023-12-04 14:56:12 26 4
gpt4 key购买 nike

Spring 4.3.2

我需要手动调用 SmartValidator.validate() 并且我需要它利用我在目标实体上定义的验证组。 javadoc 说这个...

"This variant of validate() supports validation hints, such as validation groups against a JSR-303 provider (in which case, the provided hint objects need to be annotation arguments of type Class)."


void validate(Object target,
Errors errors,
Object... validationHints)

出于某种原因,我找不到关于使用“validationHints”的太多信息或示例。所以我一直在尝试以下事情......
validator.validate(targetEntity, errors, new Class[]{ValidationGroup1.class});

validator.validate(targetEntity, errors, ValidationGroup1.class);

到目前为止,它只是完全忽略了我的分组。它总是调用所有验证器。有任何想法吗?

谢谢!

====================================

更新:javadoc 也说了这个..

"Note: Validation hints may get ignored by the actual target Validator, in which case this method should behave just like its regular Validator.validate(Object, Errors) sibling."



这听起来像是正在发生的事情。但它没有给出任何关于为什么它可能会忽略它的线索。

最佳答案

那好吧。
似乎“答案”是不要为此使用 Spring。这是我的解决方法...

import javax.validation.Validator;
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation> violations = validator.validate(targetEntity, new Class[]{group1.class, group2.class});

然后我将 Set 转换为 Spring FieldErrors(因为一切都已配置为运行 Spring)。有点像 clusterf ***,但至少它现在可以工作了。

关于spring - SmartValidator - 使用组手动调用验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209644/

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