One goal of JAX-RS 2.0 was to integerate CDI and substitute the old @Context
with the common @Inject
injection. But if I look at the JSR 339 this is not mentioned. So what is actually the CDI integration? Would there be any advantage of using JAX-RS with CDI instead of Google Guice?
@Context
is still the official way of doing injection in JAX-RS 2.0. The specification does however state that an implementation MAY make use of @Inject
alongside @Context
(from section 10.2.5 of JAX-RS 2.0 EDR 3):
Implementations MUST NOT require use of
@Inject
or@Resource
to trigger injection of JAX-RS annotated fields or properties. Implementations MAY support such usage but SHOULD warn users about non-portability.
Therefore it is not certain that all implementations of JAX-RS 2.0 supports @Inject
, and incompatibility might arise if one wants to change JAX-RS 2.0 implementation.
I did a little research and it seems Jersey 2.0 just got support for @Inject
at least.
I guess the advantage in using @Inject
instead of @Context
is that it is more consistent alongside all the @Inject
's of non JAX-RS resources one would have in a JAX-RS web-service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With