# Integrating jersey with hibernate validator cdi

**URL:** https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173
**Category:** Hibernate Validator
**Created:** [August 4, 2018, 10:10pm UTC](https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173 "2018-08-04T22:10:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mulderbaba](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mulderbaba/32/251_2.png) [@mulderbaba](https://discourse.hibernate.org/u/mulderbaba)
#### Post date: [August 4, 2018, 10:10pm UTC](https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173/1 "2018-08-04T22:10:28Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)
#### Post date: [August 6, 2018, 8:24am UTC](https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173/2 "2018-08-06T08:24:24Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![mulderbaba](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mulderbaba/32/251_2.png) [@mulderbaba](https://discourse.hibernate.org/u/mulderbaba)
#### Post date: [August 6, 2018, 10:36am UTC](https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173/3 "2018-08-06T10:36:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)
#### Post date: [August 6, 2018, 11:15am UTC](https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173/4 "2018-08-06T11:15:27Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![mulderbaba](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mulderbaba/32/251_2.png) [@mulderbaba](https://discourse.hibernate.org/u/mulderbaba)
#### Post date: [August 13, 2018, 3:23pm UTC](https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173/5 "2018-08-13T15:23:44Z")

</div>

I moved on by copying the code for now, commit can be found [here](https://github.com/payara/patched-src-jersey/commit/979c2e9cfc35c774f33a61fe8aa3a492b7a1180c).

---

<div class="post-metadata">

### Author: ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)
#### Post date: [August 13, 2018, 4:07pm UTC](https://discourse.hibernate.org/t/integrating-jersey-with-hibernate-validator-cdi/1173/6 "2018-08-13T16:07:52Z")

</div>

@mulderbaba thanks for following up!
