Also as a bonus there is a JSP and a Servlet.
A working example of a Springboot equivalent for CDI is neither as easy to find nor construct as one might expect.
Nevertheless, this application demonstrates more or less the minimum needed to have CDI (Weld), Tomcat (embedded) and RESTEasy all running in harmony.
From the weld-boot project fodler, build and package with the plugin already provided in the POM:
mvn package
Then you may run (from the project root directory) with:
target\bin\webapp.bat
You can of course run from an IDE. Just locate launch.Main.java
.
Go to http://localhost:8889/ to start with the home page.
Some reasonably essential reading:
- The best example of getting an embedded Tomcat up and running by far are the instructions provided by Heroku.
- Chapter 18. Application servers and environments supported by Weld has everything you need but examine every line carefully to be sure you don't miss an essential piece of configuration. Also pay attention to a JBoss wiki article: RESTEasy-CDIIntegration.
- RESTEasy documentation 3.0.13.Final. Pay attention to how to configuring RESTEasy when not using JBoss.
Notes on the experience:
- A good approach is to clone the example provided courtesy of Heroku on Github. Start with that, get it working and then modify as necessary.
- Jersey: Painful to merely get the correct dependencies in a CDI environment. Abandoned and went with RESTEasy.
- RESTEasy-CDI Integration gives a clue about getting RESTEasy and CDI working on Pre-Servlet 3 containers. It was necessary to have a
web.xml
in place in order for JAX-RS managed entities to permit injection of CDI managed resources. Chapter 40. CDI Integration explains why CDI and JAX-RS are not compatible by default and thus it becomes necessary to bridge the gap with the resteasy-cdi module. - Weld is supposed to work with Jetty but documentation and online material seems to be a little thin. Most CDI users are likely to be enterprise customers deploying to JBoss or some other JEE container and so most of the material online will help with these cases. Very little on Jetty but more on Tomcat so go with Tomcat, which can be embedded just as Jetty can.
- Example in the Weld documentation for embedding Tomcat just did not work.
org.jboss.weld.environment.servlet.Listener
is not required at all despite the fact that it states "With embedded Tomcat it is necessary to register Weld's listener programmatically". But then notice how the pertinent line in the code example is commented out. It almost looks like a mistake and therefore tempting to uncomment. If you copied and pasted blindly you would be fine but in any case it appears not to be required. It was necessary however to have/WEB-INF/beans.xml
and/META-INF/context.xml
in place. - JMS with JNDI documentation sparse for Tomcat and embedded Tomcat at that. Need to configure web.xml and context.xml and enable JNDI (tomcat.enableNaming()).