CDI, JAX-RS and dependent libraries support

Table of Contents

Introduction

CDI and JAX-RS are dependencies for many other APIs and libraries. This guide explains how to add support for them in Tomcat using two optional modules that are provided in the Tomcat sources.

CDI support

CDI support is provided by the modules/owb optional module. It packages Apache OpenWebBeans and allows adding CDI support to the Tomcat container. The build process of the module uses Apache Maven, and is not available as a binary bundle as it is built using a number of publicly available JARs.

Building

Building the module requires Apache Maven, a JDK supported by the Tomcat version used by the module, and the Tomcat artifacts for that version in the local Maven repository. Tomcat 12 requires Java 21; Tomcat 11 requires Java 17. The CDI APIs used by the module require Java 17.

The process to build CDI support is the following.

cd $TOMCAT_SRC/modules/owb
mvn clean && mvn package
The resulting JAR at target/tomcat-owb-x.y.z.jar (where x.y.z depends on the Apache OpenWebBeans version used during the build) can be placed directly into the lib folder of the Tomcat installation. The JAR is built against Jakarta EE APIs. The Tomcat migration tool for Jakarta EE is only required for applications that still use javax APIs.

Testing

The module integration tests use an embedded Tomcat server and run against the shaded module JAR. They are Failsafe integration tests and are not run by mvn package.

To build the module and run all integration tests:

cd $TOMCAT_SRC/modules/owb
mvn clean verify
A single integration test can be run with:
mvn -Dit.test=OpenWebBeansSecurityIT verify

The tests cover CDI APIs, both supported listener configuration modes, beans.xml discovery, security integration, lifecycle callbacks and JSP/EL integration. See modules/owb/README.md for the test architecture and for the prerequisites needed when Tomcat snapshot artifacts are used.

Enabling CDI support

CDI support can be enabled for all webapps in the container by adding the following listener in server.xml nested inside the Server element:

<Listener className="org.apache.webbeans.web.tomcat.OpenWebBeansListener" optional="true" startWithoutBeansXml="false" />
The listener will produce a non fatal error if the CDI container loading fails.
The listener supports the following properties:

  • startWithoutBeansXml - if true, CDI is started even when no beans.xml is present. The default is true.
  • addSecurityValve - if true, the OpenWebBeansSecurityValve is added to each context pipeline. The default is true.

CDI support can also be enabled at the individual webapp level by adding the following listener to the webapp context.xml file nested inside the Context element:

<Listener className="org.apache.webbeans.web.tomcat.OpenWebBeansContextLifecycleListener" />
The context-level listener supports the same properties as the server-level listener.

When security integration is required, the listener can add the OpenWebBeansSecurityValve automatically. Alternatively, the OpenWebBeansSecurityFilter can be configured in the webapp WEB-INF/web.xml file.

Known limitations

Servlet, filter and ServletContextListener field injection are supported when CDI support is enabled.

EL method invocation against a normal-scoped CDI proxy bean is not supported; use property access or a dependent bean instead.

Support for features that depend on optional CDI EL APIs or on CDI features not implemented by OpenWebBeans may vary. See modules/owb/README.md for the currently tested feature set.

JAX-RS support

JAX-RS support is provided by the modules/cxf optional module. It packages the Apache CXF project and allows adding JAX-RS support to individual webapps. The build process of the module uses Apache Maven, and is not available as a binary bundle as it is built using a number of publicly available JARs. The support depends on CDI support, which should have previously been installed at either the container or webapp level.

The process to build JAX-RS support is the following.

cd $TOMCAT_SRC/modules/cxf
mvn clean && mvn package
The resulting JAR at target/tomcat-cxf-x.y.z.jar (where x.y.z depends on the Apache CXF version used during the build) should then be placed into the /WEB-INF/lib folder of the desired web application.

If the CDI support is available at the container level, the JAR can also be placed in the Tomcat lib folder, but in that case the CXF Servlet declaration must be individually added in each webapp as needed (it is normally loaded by the web fragment that is present in the JAR). The CXF Servlet class that should be used is org.apache.cxf.cdi.CXFCdiServlet and should be mapped to the desired root path where JAX-RS resources will be available.