CDI-Injection is JAX-RS 2.1 (Java EE 8) Subresources

A JAX-RS subresource:

@Path("sub")
public class SubResource {

    @Inject
    Facade facade;

    @GET
    public String sub() {
        return this.facade.sub();
    }
}
with injected @Stateless

"business logic":

@Stateless
public class Facade {

    public String root() {
        return "root";
    }

    public String sub() {
        return "sub";
    }
}

and exposed via a root resource:

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;

@Path("root")
public class RootResource {

    @Context
    ResourceContext context;


    @Path("sub")
    public SubResource sub() {
        return this.context.getResource(SubResource.class);
    }
}

supports CDI injection in WildFly 15+, OpenLiberty 18.0.0.3+, Payara 5+ as "nice to have" feature.

A curl invocation curl -i http://localhost:[PORT]/subresource-cdi-di/resources/root/sub returns: "sub" .

The JAX-RS 2.1 spec states in 11.2.3 Context and Dependency Injection (CDI) :

"In a product that supports CDI, implementations MUST support the use of CDI-style Beans as root resource classes, providers and Application subclasses.(...)"

Therefore the beviour of TomEE 8 (microprofile-8.0.0-M1) is correct by throwing:

java.lang.NullPointerException
com.airhacks.subresource.boundary.SubResource.sub(SubResource.java:20)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Deployed to multiple application servers simultaneously with: WAD

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting . Is Munich's airport too far? Learn from home: airhacks.io .

NEW: Online Workhopis also coming to:

.