Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The JAX-RS 2.0 specification (JSR-339) mandates the support of CDI 1.1 (JSR-346) and Apache CXF starting from the version 3.0 introduces the initial support of this feature. As the starting point, the emphasis has been done on supporting embedded Jety 8/9 and Tomcat 7/8 containers as primary deployment (though other application servers will be supported in the future). 

...

At the moment, the integration of Apache CXF and CDI revolves around two key components, residing which reside in the new module called cxf-integration-cdi

...

If the Apache CXF application contains only either no instance of JAX-RS Application or only one single instance of JAX-RS Application (annotated with @ApplicationPath) with no singletons and classes defined, the following rules are being applied by JAXRSCdiResourceExtension in order to configure and publish the configured JAX-RS resources:

...

Code Block
java
java
@Path("/bookstore/")
public class BookStore {
    @Inject private BookStoreService service;
    
    @GET
    @Path("/books/{bookId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Book getBook(@PathParam("bookId") String id) {
        return service.get(id);
    }
}

Additional Configuration Capabilities

If you have a need to, you can configure the underlying JAXRSServerFactoryBean by implementing the interface JAXRSServerFactoryCustomizationExtension.  Instances of this extension can be located via ServiceLoader of as CDI beans.  You can use this to programmatically add new providers, or otherwise manipulate the runtime before the server is created.  An example of how this is used can be seen at SseTransportCustomizationExtension.

CDi Lifecycle for JAX-RS Context Objects

The CDI extension also supports the injection (via CDI) of @Context objects.  Any known object can be injected via CDI.  This happens automatically when the CDI extension is used, and a @Context is declared as a field on a resource class.  Note that this does allow you to inject your @Context objects in non-JAX-RS components as well.  The logic is in two parts:

  1. Rewriting injection points
    1. When an injection point is found that contains @Context two additional annotations are added
    2. These annotations are @Inject and @ContextResolved
    3. The use of @Inject will force the CDI runtime to take over the injection of the bean value, while @ContextResolved is simply a qualifier we apply to the injection point to avoid ambiguities
  2. Providing Beans
    1. For each of the built in @Context types, we register a bean that provides a @RequestScoped bean that resolves the value, by looking up the internal context value
    2. In addition, a user or intergrator can implement ContextClassProvider to register an additional class as context type to be resolved.

 

Deploying with embedded Jetty 8/9 (programmatic configuration)

...

Code Block
java
java
final Tomcat server = new Tomcat();
server.setPort(<port>);

final File base = createTemporaryDirectory();
server.setBaseDir(base.getAbsolutePath());    
    
server.getHost().setAppBase(base.getAbsolutePath());
server.getHost().setAutoDeploy(true);
server.getHost().setDeployOnStartup(true);
            
server.addWebapp(<context path>, <path to WAR folder/file>);   
server.start();

 

Future work

The Apache CXF team is committed to improve the CDI integration and to cover more deployment scenarios and wide range of application configurations and demands. However, by and large the CDI integration is implementation-independent and it is expected to work in JBoss WildFly or other EE containers.