Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of using JAX-RS 2.0 with CDI/Weld instead of Guice?

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?

like image 824
deamon Avatar asked Oct 08 '22 07:10

deamon


1 Answers

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

like image 127
uldall Avatar answered Oct 12 '22 12:10

uldall