What is the problem?

You want to write a Java app, that has all the good stuff in it?

By good stuff I mean:

  • CDI (who doesn’t want awesome DI and interceptors)
  • JAX RS
  • JSF
  • JPA 2.0 (with container managed transactions)

Of course you do, why wouldn’t you? I love an app server as much as the next guy or gal, but it is another piece of the stack which needs to be monitored and patched, it can also have its own bag of issues you need to look out for.

So we recently wrote a product and I wanted to use all the “good stuff”. Since the product is to be widely deployed I veto’d the use of an app server after all less moving parts means less support calls.

So I went about getting all these goodies working in Java SE and here is how I did it.

A fully working project to go along with this post is available here.

Servlet Container

You’re going to need one of these, I picked Jetty 8 its really easy to get running in embedded mode and it has JNDI. That said you could probably use Tomcat 7 without too much modification.

The above is some pretty standard stuff to get embedded jetty going.

CDI Container

You’ll also be needing one of these, I chose Weld for the job. Your web.xml would need to look something like the snippet below.

The Weld servlet listener will start our CDI container and the init() method of AppServlet is our entry point into our application after the containers have loaded, so you can go ahead and put any initialization you want in here with the CDI container available!

Next we have our CDI bean.

Our CDI interceptor implementation…

… and the interceptor annotation.

We even have an Arquillian unit test.

In part 2 we will get JAX RS working as well.