Skip to content

JAX-RS resources CDI injection does not work if the JAX-RS annotations are located in the implemented interface. #34274

@dgeissl

Description

@dgeissl

Describe the bug
We're using swagger to create interfaces for our rest services. These interfaces already contain all necessary JAX-RS annotations, that's why the implementation classes do not need/want to repeat them.

If such a REST service now tries CDI Injection of EJBs or other beans, the injection does not work and the property is still null when the service is called. The registration as REST service did complete successfully as we can see it being called.

The issue started when we where switching from javaee-7.0 to jakartaee-10.0.

Steps to Reproduce

Use the jakartaee-10.0 feature in openliberty.
Create an Interface & JAXRS service:

import jakarta.ws.rs.*;
@Path("/clock")
public interface ClockService  {
    @GET @Path("/now") @Produces({ "application/json" })
    public Response getTimestamp();
}
---
import jakarta.ws.rs.*;
import jakarta.inject.Inject;

public class ClockServiceIml implements ClockService {
    @Inject
    private TimeService timeService;

    public Response getTimestamp(){ 
        return Response.ok(timeService.getTime());
    }
}

Variations:

  • We tried to inject @Stateless session beans and @Named CDI beans, the result was always null
  • Repeating the @Path annotation from the Interface in the implementation class this resolve the issue (however this would introduce duplicates between the swagger file and the code which can easily cause conflicts and hard to track bugs)
@Path("/clock") // injected service is no longer null
public class ClockServiceIml implements ClockService {
  • making the JAXR-RS resource a @Stateless session bean resolved the injection issue too, at the cost of having to now make all jars an EJB module.
@Stateless // injected service is no longer null
public class ClockServiceIml implements ClockService {
  • Specifically marking the JAX-RS resource with @Named still resulted in NPE.
@Named // still NPE on the injected service
public class ClockServiceIml implements ClockService {

Expected behavior
The TimeService should be injected and I can call methods on it.

Diagnostic information:

  • OpenLiberty Version: 26.0.0.2
  • Affected feature(s) jakartaee-10.0
  • Java Version: openj9-17

Additional context
OpenLiberties documentation https://openliberty.io/docs/latest/jaxrs-integration-cdi.html on JaxRs and CDI does not show any evidence as to why this should not work.

Metadata

Metadata

Assignees

Labels

AcknowledgedAn initial response has been provided. Remove the 'Needs member attention' label.Opened by external contributorin:JAX-RSrelease bugThis bug is present in a released version of Open Liberty

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions