通过servicelododer定义的约束效果剂的不一致的豆验证初始化

发布于 2025-02-10 18:30:57 字数 2520 浏览 0 评论 0 原文

这个问题询问了一些有关豆验证模块化的更多详细信息,我问之前的验证c“>。

在上面链接的问题上,以下是 /a> i将注释和约束视频定义分为2个Java模块,并使用Serviceloader将它们链接在一起,如文档中所示在这里。作品主要是。但是有一个尚未解决的问题,它不起作用通过XML定义的验证,我根据 documentation 再次。 什么不起作用:未设置注释和约束效果之间的配对,根本不使用服务加载器的东西。

回顾一下:我使用此ServiceloDoder方法进行了工作设置,并且在验证通过REST层的内容时起作用。所有配对正确。

但!我们也通过Kafka获得了这些DTO。在这里,我们有两个不同的流动。在启动时,常见的约束效能器有一些初始化,然后:

  1. 如果我们首先收到REST消息,仅在此请求时间发现ServicelOderoder的内容,似乎进行了一些下一个初始化,在此之后,即使是KAFKA消息也可以使用,这意味着配对以进行自定义验证器配对。无处不在。 (很好!),
  2. 如果首先到达Kafka消息(典型),则不会咨询服务加载程序的内容,并以某种方式以某种方式“摧毁”配置,即使以后的休息请求来了给定注释没有约束效果。初始化以某种方式缺陷完成。

验证。xml与:

<validation-config
    xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration validation-configuration-2.0.xsd"
    version="2.0">

  <constraint-mapping>/META-INF/validation-constraints.xml</constraint-mapping>
</validation-config>

注释:

  • 2.0版本一样容易,是由于来自春季依赖关系管理的Hibernate-validator 6.2.0。
  • 为什么不使用注释并完全抛弃此XML东西?不是我的文件,无法解码。

如果有一些微不足道的新手错误,请告知。也许有某种方法可以将服务加载器功能启动到validation.xml文件中的操作中,我不知道并且找不到任何地方。


编辑/建议:

答:尝试在启动上注入验证器以确保其加载:

@Autowired
private Validator validator;
@EventListener(ApplicationReadyEvent.class)
public void logReady() {
    System.out.println(validator.toString());
}

是否打印了初始化的验证器,但没有帮助。

This question asks for some specifics about more general topic regarding modularization of bean validation I asked before.

In question linked above, following this documentation and this post I split annotation and ConstraintValidator definition into 2 java modules, and linked them together using ServiceLoader as shown in documentation here. Works, mostly. But there is one unsolved issue, that it does not work for validation defined via XML, which I did according to documentation again. What does not work: The pairing between annotation and ConstraintValidator is not set, the service loader stuff is not used at all.

To recap: I have working setup using this ServiceLoader approach and it works when validating stuff coming through rest layer. All paired correctly.

BUT! We are getting these DTOs also through kafka. And here we have two different flows. There is some initialization of common ConstraintValidators on startup, and then:

  1. if we first get REST message, ServiceLoader stuff is discovered only at this request time, some next initialization is done seemignly, and after that even kafka messages works, meaning pairing for custom validator is available everywhere. (Great!)
  2. if kafka message arrives first though(typical), no service loader stuff is consulted and somehow it 'destroys' the configuration in way, that even if later rest request comes it won't work either, saying, that there is no ConstraintValidator for given annotation. The initialization is completed somehow defectively.

validation.xml is as easy as:

<validation-config
    xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration validation-configuration-2.0.xsd"
    version="2.0">

  <constraint-mapping>/META-INF/validation-constraints.xml</constraint-mapping>
</validation-config>

notes:

  • 2.0 version is because of hibernate-validator 6.2.0 which comes from spring dependency management.
  • Why not use annotation and dump this xml stuff altogether? Not mine file, unmodifiable.

If there is some trivial newbie mistake, please advise. Maybe there is some way how to kick in service loader functionality into action in validation.xml file, I'm not aware of and cannot find anywhere.


EDITS/suggestions:

A: try to inject validator on startup to make sure it's loaded:

@Autowired
private Validator validator;
@EventListener(ApplicationReadyEvent.class)
public void logReady() {
    System.out.println(validator.toString());
}

did print initialized validator, did not help though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尴尬癌患者 2025-02-17 18:30:57

我忘记了这个问题,我再一次问了这个问题在这里,但是这次我设法创建了MWE,并且行为被确认为Hibernate验证器 bug

I forgot about this question and I asked this question once more here, but this time I managed to create MWE and behavior was confirmed as Hibernate Validator bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文