Skip to content

Instantly share code, notes, and snippets.

@klausbrunner
Last active February 11, 2016 16:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klausbrunner/9349541 to your computer and use it in GitHub Desktop.
Save klausbrunner/9349541 to your computer and use it in GitHub Desktop.
Using Weld 2.x as a CDI 1.1 implementation on Jetty 9.1, and Jersey 2.x for JAX-RS 2.0 on top of that

Servlets with CDI

  • get latest Jetty 9.1 archive and extract somewhere (I'll assume the Jetty root is called "jetty")
  • in jetty/lib, create a folder called "weld" and download the weld-servlet and weld-servlet-core JARs into it. For injection in listeners to work, version should be 2.2.0.Beta1 or later.
  • in jetty/modules, create a weld.mod as in this gist
  • in jetty/start.ini, add a single line containing "--module=weld" to initialise Weld

In most cases, that should be it and you can deploy any compliant CDI application, even without a web.xml. In some cases though you may need to add the old-fashioned filter entry to the web.xml. See also: weld/core#465

Servlets with CDI and JAX-RS 2.0 (using Jersey)

  • get jersey-container-servlet, jersey-client (if you need it), and jersey-gf-cdi including all their dependencies and put them into a jetty/lib/jersey folder
  • add a jetty/modules/jersey.mod file (similar to the Weld one) and activate the module with "--module=jersey"

This seems to work at least for a basic CDI+JAX-RS application: https://github.com/KlausBrunner/jaxrscditest

Note that WARs aren't properly classloader-separated if used in this way! Another option is to add the required JARs to the context XMLs of each WAR; see XMLs in this gist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- quickly hacked context xml to add Weld JARs to a WAR -->
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="war">/home/klaus/cditest/target/cditest.war</Set>
<Set name="contextPath">/cditest</Set>
<Call name="prependServerClass"><Arg>-org.eclipse.jetty.</Arg></Call>
<Set name="extraClasspath"><SystemProperty name="jetty.home" default="."/>/lib/weld/weld-servlet-2.2.0.Beta1.jar,<SystemProperty name="jetty.home" default="."/>/lib/weld/weld-servlet-core-2.2.0.Beta1.jar</Set>
</Configure>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="war">/home/klaus/jaxrscditest/target/jaxrscditest.war</Set>
<Set name="contextPath">/jaxrscditest</Set>
<Call name="prependServerClass"><Arg>-org.eclipse.jetty.</Arg></Call>
<Set name="extraClasspath">
<SystemProperty name="jetty.home" default="."/>/lib/weld/weld-servlet-2.2.0.Beta1.jar,
<SystemProperty name="jetty.home" default="."/>/lib/weld/weld-servlet-core-2.2.0.Beta1.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/aopalliance-repackaged-2.2.0.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/hk2-api-2.2.0.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/hk2-locator-2.2.0.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/hk2-utils-2.2.0.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/javassist-3.18.1-GA.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/javax.annotation-api-1.2.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/javax.inject-2.2.0.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/javax.ws.rs-api-2.0.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/jersey-client-2.6.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/jersey-common-2.6.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/jersey-container-servlet-2.6.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/jersey-container-servlet-core-2.6.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/jersey-guava-2.6.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/jersey-server-2.6.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/osgi-resource-locator-1.0.1.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/validation-api-1.1.0.Final.jar,
<SystemProperty name="jetty.home" default="."/>/lib/jersey/jersey-gf-cdi-2.7-SNAPSHOT.jar <!-- this is a patched JAR without an interceptor that triggers strange "no binding" errors -->
</Set>
</Configure>
These are the contents of jetty/lib/jersey using Jersey 2.6:
aopalliance-repackaged-2.2.0.jar
hk2-api-2.2.0.jar
hk2-locator-2.2.0.jar
hk2-utils-2.2.0.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.2.0.jar
javax.ws.rs-api-2.0.jar
jersey-client-2.6.jar
jersey-common-2.6.jar
jersey-container-servlet-2.6.jar
jersey-container-servlet-core-2.6.jar
jersey-gf-cdi-2.6.jar
jersey-guava-2.6.jar
jersey-server-2.6.jar
osgi-resource-locator-1.0.1.jar
validation-api-1.1.0.Final.jar
#
# Weld Module
#
[depend]
jndi
annotations
plus
[lib]
lib/weld/*.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment