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).
Architecture and design
At the moment, the integration of Apache CXF and CDI revolves around two key components, which reside in the new module called cxf-integration-cdi
- CXFCdiServlet servlet
- JAXRSCdiResourceExtension portable CDI extension
The fact of including cxf-integration-cdi as a dependency allows JAXRSCdiResourceExtension portable CDI extension to be discovered by CDI container. The JAXRSCdiResourceExtension creates the instance of the Bus and registers it with BeanManager. From this point, the Bus instance is a regular CDI bean (with @Application scope) available for injection. This instance of the Bus is being injected into CXFCdiServlet servlet once it is initialized by servlet container.
Depending on the design, JAXRSCdiResourceExtension may use zero-based configuration approach or rely on particular JAX-RS Application instances. The org.apache.cxf.cdi.CXFCdiServlet should be configured as well (more examples for programmatic and WAR scenarios below).
If the Apache CXF application contains 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:
- the instance of the JAX-RS Application (annotated with @ApplicationPath) is being created and registered with BeanManager
- all instances of the discovered JAX-RS providers (annotated with @Provider) are being created and registered with BeanManager
- all instances of the discovered JAX-RS resources (annotated with @Path) are being created and registered with BeanManager
Lastly, the instance of the JAXRSServerFactoryBean is being created and configured with all service beans and providers discovered before. Additionally, the providers are enriched with the services for javax.ws.rs.ext.MessageBodyReader and javax.ws.rs.ext.MessageBodyWriter, loaded via ServiceLoader. From this moment, Apache CXF application is ready to serve the requests. The quick example is shown below.
The empty JAX-RS Application:
And one JAX-RS resource:
If the Apache CXF application contains one or more instances of JAX-RS Application (annotated with @ApplicationPath) with singletons or classes defined, the following rules are being applied by JAXRSCdiResourceExtension in order to configure and publish the configured JAX-RS resources:
- the instance of each JAX-RS Application (annotated with @ApplicationPath) is being created and registered with BeanManager
- for each JAX-RS Application instance created before, the instance of the JAXRSServerFactoryBean is being created and configured in a way that application's singletons/classes are being splitted to providers (annotated with @Provider), service beans (annotated with @Path) and features (implementation of org.apache.cxf.feature.Feature)
From this moment, Apache CXF application is ready to serve the requests. The quick example is shown below.
The configured JAX-RS Application:
And one JAX-RS resource:
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:
- Rewriting injection points
- When an injection point is found that contains @Context two additional annotations are added
- These annotations are @Inject and @ContextResolved
- 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
- Providing Beans
- 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
- 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)
With Jetty 8/9 it possible to create fully embeddable REST / JAX-RS servers without web.xml or WAR files involved. For Apache CXF applications which are using CDI 1.1, the CXFCdiServlet servlet should be used as a starting point. Following example demonstrates the necessary configuration points in order to create embedded Jetty 8/9 instance. As a CDI 1.1 implementation, JBoss Weld 2.0 is being used.
This code snippet is enough to trigger the CDI portable extension discovery, to perform the configuration (depending on JAX-RS Applications present) and to wire up all defined dependencies together.
Deploying with embedded Jetty 8/9 (WAR-based deployment)
Another option to deploy Apache CXF application with CDI 1.1 support and embedded Jetty 8/9 is by using web.xml descriptor and WAR-like deployment structure. In this case, the Apache CXF application needs to declare CXFCdiServlet servlet (and its mappings) and, if required, CDI-specific listeners (in the example below the JBoss Weld 2.0 is being used as CDI 1.1 container).
The server initialization in this case looks simpler.
Please notice, usage of Jetty-specific server classes ("org.eclipse.jetty.servlet.ServletContextHandler.Decorator") is very important to allow CDI 1.1 injections (backed by JBoss Weld 2.0) to work seamlessly across servlets / listeners / filters. It is not stricktly necessary for Apache CXF (everything will work as expected) but complex applications would definitely benefit from that.
Deploying with embedded Tomcat 7/8 (WAR-based deployment)
In case of embedded Tomcat 7/8, Apache CXF application with CDI 1.1 support could be deployed with web.xml descriptor and WAR-like deployment structure, similarly to Jetty 8/9 WAR-based deployment. Apache CXF application needs to declare CXFCdiServlet servlet (and its mappings) and, if required, CDI-specific listeners (in the example below the JBoss Weld 2.0 is being used as CDI 1.1 container).
Tomcat 7/8 server has a different API, by still quite simple initialization procedure.
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.