jakartaee / rest

Jakarta RESTful Web Services
Other
362 stars 117 forks source link

Turn @Path annotation into a CDI Stereotype #556

Open glassfishrobot opened 7 years ago

glassfishrobot commented 7 years ago

As suggested on the mailing list here and here.

This is a sub-issue of #60.

In order to turn any resource class annotated with @Path into a CDI bean, the @Path annotation can be turned into a CDI Stereotype with the @RequestScoped scope default. This doesn't require a runtime dependency on CDI at all and wouldn't introduce any changes to JAX-RS API or implementations outside of a CDI container.

CDI API would be needed only to compile JAX-RS API because annotations not found on the classpath are ignored by the JVM, CDI impl wouldn't be needed at all. If we specify @RequestScoped in the stereotype, it would just specify what I believe all implementations do anyway -> create a resource instance per request, which is the default behavior mandated by the previous JAX-RS 2.0 It would be possible to override the scope for each class in a standard way by providing a scope annotation together with @Path.

Furthermore, if @Path is a stereotype, it would also become a bean-defining annotation and turn on CDI scanning for the module by default, even if there are no other CDI beans in the module, which is what most people would expect. Again, outside of a CDI container, the additional annotations on the @Path annotation would be ignored. They can even be missing in the classpath at runtime and would still be just ignored without any errors.

glassfishrobot commented 6 years ago
mkarg commented 6 years ago

@arjantijms As this was originally opened by @OndrejM, may I assign this issue to you so you can further drive this item in his name?

arjantijms commented 6 years ago

Sure, and I'll discuss it with Ondrej if/when needed.

spericas commented 6 years ago

This should probably be a 3.0 issue given that CDI isn't mandatory now.

ggam commented 6 years ago

CDI integration is one of those changes that affect most people, and I still think it can be done in a minor revision.

Annotating a class at compile time doesn't make the the annotation required on the classpath at runtime (the JVM just ignores it) so we could find a solution that works without mandating CDI.

Another possibility is for implementors to register the resources as beans via a CDI extension. Extensions are loaded by CDI so they wouldn't be loaded if CDI is not on the classpath. (This approach is not always feasible though as you need to know the classes before ServletContainerInitializers are called, which is not the case of Jersey AFAIK).

JAX-RS 2.1 specified that managed executor services must be used in case the runtime supports JSR-236. The same can be done here without pulling a runtime dependency on CDI.

El vie., 13 abr. 2018 16:05, Santiago Pericas-Geertsen < notifications@github.com> escribió:

This should probably be a 3.0 issue given that CDI isn't mandatory now.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eclipse-ee4j/jaxrs-api/issues/556#issuecomment-381145982, or mute the thread https://github.com/notifications/unsubscribe-auth/ACAucEhHUdigUDComxgaBfxyBIJYshRUks5toLCTgaJpZM4TLt7I .

chkal commented 6 years ago

Annotating a class at compile time doesn't make the the annotation required on the classpath at runtime (the JVM just ignores it) so we could find a solution that works without mandating CDI.

Is this the case even for @Retention(RUNTIME)?

ggam commented 6 years ago

Yes, although it's a little known behavior. See this Stackoverflow questions for some more details: https://stackoverflow.com/questions/3567413/why-doesnt-a-missing-annotation-cause-a-classnotfoundexception-at-runtime

El sáb., 14 abr. 2018 10:58, Christian Kaltepoth notifications@github.com escribió:

Annotating a class at compile time doesn't make the the annotation required on the classpath at runtime (the JVM just ignores it) so we could find a solution that works without mandating CDI.

Is this the case even for @Retention(RUNTIME)?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/eclipse-ee4j/jaxrs-api/issues/556#issuecomment-381314845, or mute the thread https://github.com/notifications/unsubscribe-auth/ACAucK0uVGLOppzKo9hUkCO4pjmAsCOAks5tobo2gaJpZM4TLt7I .

chkal commented 6 years ago

@ggam Oh, interesting. I didn't know about this before. Thanks.