RESTEasy,CDI,嵌入式Jetty,bean验证被忽略 [英] RESTEasy, CDI, embedded Jetty, bean validation is ignored
问题描述
我有一个Groovy项目,我使用RESTEasy和Weld并将其部署到嵌入式Jetty。我似乎无法工作的是bean验证。 RESTEasy文档说,添加 resteasy-validator-provider-11
以及hibernate验证器依赖项( hibernate-validator,hibernate-validator-cdi,javax.el -api,javax.el
)就足够了。但是RESTEasy简单地忽略了bean验证。我好奇地也在日志中得到以下消息:
plugins.validation.ValidatorContextResolver - 无法找到支持ValidatorFactory的CDI。使用默认的ValidatorFactory
基于[this] [1]文章的建议,我试着注册Hibernate InjectingConstraintValidatorFactory
在META-INF / validation.xml中,但它取决于一个正在被注入的BeanManager,并在运行时崩溃。
代码可以在这里找到 https://github.com / abhijitsarkar / groovy / tree / master / movie-manager / movie-manager-web
一个日志要点在这里: https://gist.github.com/anonymous/8947319
我已经尝试了一切在阳光下没有任何成功。请帮助。
我终于解决了这个问题。事实证明,validation.xml并不是必需的, resteasy-cdi
模块在注册BeanManager方面做得很好。我遗漏的东西并没有清楚地记录在任何地方,如果一个注释放在一个方法上,验证引擎只是决定什么应该被验证。我在一个方法上放置了一个 @NotNull
,它正在验证返回类型,而不是参数。可以在某些情况下使用 validationAppliesTo
元素,但是 @NotNull
没有它。当我将它从方法移动到参数时,它开始工作。
现在我遇到了我认为是焊接错误的问题,但我会单独发布该问题。
I've a Groovy project where I use RESTEasy with Weld and deploy to embedded Jetty. What I can't seem to get working is bean validation. RESTEasy documentation says that adding resteasy-validator-provider-11
along with hibernate validator dependencies (hibernate-validator, hibernate-validator-cdi, javax.el-api, javax.el
) is enough. But the bean validation is simply ignored by RESTEasy. I curiously also get the following message in the logs:
plugins.validation.ValidatorContextResolver - Unable to find CDI supporting ValidatorFactory. Using default ValidatorFactory
Based on the suggestions on [this][1] post, I tried registering Hibernate InjectingConstraintValidatorFactory
in META-INF/validation.xml but it depends on a BeanManager being injected and blows up at runtime.
The code can be found here https://github.com/abhijitsarkar/groovy/tree/master/movie-manager/movie-manager-web
A log gist is here: https://gist.github.com/anonymous/8947319
I've tried everything under the sun without any success. Pls help.
I finally fixed this. Turns out, a validation.xml is not really required, resteasy-cdi
module does a fine job of registering the BeanManager. What I was missing and not clearly documented anywhere, is that if an annotation is placed on a method, the validation engine just "decides" what should be validated. I placed a @NotNull
on a method and it was validating the return type, not the parameters. One can use validationAppliesTo
element in some cases but @NotNull
doesn't have it. When I moved it from the method to the parameter, it started working.
Now I ran across what I believe is a Weld bug but I'll post that question separately.
这篇关于RESTEasy,CDI,嵌入式Jetty,bean验证被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!