Move to Java EE 6 and slash your number of lines of code by more than half

My first reaction when I started to help Paul and the team run and test Bill Burke’s ex11_1 sample on GlassFish v3 was that this code could be refactored to be much simpler and more portable with Java EE 6 and JAX-RS 1.1. This wasn’t the best approach to show the portability of existing JAX-RS code so the porting happened with minimal (really small) changes. Yet, I wanted to share the simplifications here.

Remove web.xml altogether. The Jersey servlet no longer needs to be declared and mapped, and the EJB local references now have well known and portable names. This is replaced with a @ApplicationPath("mypath") annotation placed on the ShoppingApplication class.

Put those EJBs in a WAR. The initial project has three artifacts: WAR, EJB jar, and a global EAR, each having a separate Maven pom.xml. This can all be boiled down to a single WAR file.

Morph resources and Beans into a single class. The dichotomy between \*Resource and \*ResourceBean is no longer necessary. A resource can simply now be a transactional EJB by adding the @Stateless along side the @Path annotation.

Test. This is made easy by the simplified packaging but essentially is the same as what’s described here (uses the GlassFish Maven plugin).

The end-result has 60% less lines of code (and I’m sure you could get even remove more without hurting the code).

These are all simplifications made possible by Servlet 3.0, EJB 3.1 and JAX-RS 1.1 (all part of Java EE 6).

Author: alexismp

Google Developer Relations in Paris.

%d bloggers like this: