Integrating jersey with hibernate validator cdi


#1

So I’m trying to get jersey working with hibernate validator in order to provide in order to provide cdi bean injection into constraint validators. to do this, in the ValidationBinder class of jersey, I tried to set the org.hibernate.validator.cdi.internal.InjectingConstraintValidatorFactory as the constraintValidatorFactory of the validatorContext of jersey. That looks ok at first sight but the InjectingConstraintValidatorFactory class comes from internal package which is not being exported in OSGI and it’s not bein instantiated by HK2 as well. So what would be the best way to use this InjectingConstraintValidatorFactory class and integrate it wit jersey?


#2

Indeed InjectingConstraintValidatorFactory is not designed to be used directly as it’s an internal class.

You should use the CDI ValidationExtension itself so that the CDI magic is done and then inject the ValidatorFactory created by the extension where you need it via @Inject.


#3

Well, this is jersey code base and we only have HK2 at hand only, I’ll try to figure out a way to initialize that extension.


#4

I’m not very familiar with Jersey code but the idea would be to understand if there’s a way to use the ValidatorFactory created by our CDI module. That would definitely be the cleanest solution.

If you can’t do that, you can simply copy InjectingConstraintValidatorFactory (and remove the references to our Contracts class so that you don’t use any internal classes). It’s simple enough and rely on specified APIs so it’s something very stable.

If you can end up with a solution injecting the ValidatorFactory created by our CDI module in Jersey, please report back here as it might be useful to others.

Thanks!


#5

I moved on by copying the code for now, commit can be found here.


#6

@mulderbaba thanks for following up!