Skip to content

Embedded Tomcat, Weld and RESTEasy all playing nicely together and ready to go!

Notifications You must be signed in to change notification settings

chrisjleu/tomcat-weld-resteasy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Java 8 web application using Embedded Tomcat, Weld and RESTEasy

Also as a bonus there is a JSP and a Servlet.

Introduction

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.

Build and package

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.

Resources

Some reasonably essential reading:

Notes

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()).

About

Embedded Tomcat, Weld and RESTEasy all playing nicely together and ready to go!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published