## CDI, JAX-RS and dependent libraries support

### Table of Contents

- Introduction
- CDI support
- JAX-RS support

### 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 the Apache OpenWebBeans project 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.

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) should be processed by the Tomcat migration tool for Jakarta EE, and then be placed into the `lib` folder of the Tomcat installation.
CDI support can then 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.
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 `Server` element:

    <Listener className="org.apache.webbeans.web.tomcat.OpenWebBeansContextLifecycleListener" />

### 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.