How to integrate JAX-RS with CDI in a Servlet 3.0 container

旧时模样 提交于 2019-11-29 04:02:05
Sebastian S.

I solved my problem.

The problem is, that Jersey JAX-RS implementation uses the HK2 dependency injection framework and this framework is simply not aware of the CDI beans. And by following the idea of the accepted answer in this post, I make the CDI beans available for the HK2 injection bindings and the injection of my application scoped beans works fine now.

But I really wonder why it is so cumbersome to bring together two constituent parts of Java EE.

Update: As G. Demecki mentioned in a comment, this is solution is no longer needed! But it helped me out at the time of asking this question.

You can just include this dependency to enable Jersey-CDI integration

<dependency>
    <groupId>org.glassfish.jersey.containers.glassfish</groupId>
    <artifactId>jersey-gf-cdi</artifactId>
    <version>2.2</version>
</dependency>

It works for me using Jersey 2.8, Jetty 9.1 and Weld 2.2.0.SP1.

I have created a sample project with Embedded Jetty, Jersey 2 and CDI https://drive.google.com/file/d/0B9j0dIS5bS0wSEg0NTk2eGpPSGs/view?usp=sharing

Kindly let me know if you find any issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!