rest as separate maven module

Refresh

March 2019

Views

2k time

1

i'm setting up a new multi module project structure. for now there is an ejb and an ear module and i want to add a web module for a RESTful web service. what is the best approach?

the application looks like this (modules and their content):

  • app (pom): the parent pom
    • core (ejb): ejb, hibernate, DAOs, entities, business logic
    • ear (ear): the module that creates a deployable container

now i would like to add a restful webservice module.

looking at the jboss-as-7 quickstart applications from http://www.jboss.org/jbossas/downloads there is always a war module which just bootstraps the rest service while the beans with jax-rs annotations are part of the ejb project (in my case core:

  • app
    • ejb (containing services + beans with jax-rs annotations)
    • ear
    • war (just containing a web.xml)

my issue with this approach is that the ejb module contains the services and the rest resources. wouldn't it be better to keep them separate? or is this the way to go?

otherwise: which approach would be better?

  • app
    • core
    • ear
    • rest (war which contains the rest resource beans and the web.xml)

or

  • app
    • core
    • ear
    • rest
    • rest-resources (separate container for the rest resource beans)
    • rest-war (war which only contains the web.xml)

2 answers

1

From a JEE6/JAX-RS point of view the only thing you need a WAR file for is to actually have something to hand off to the application server to deploy. It can literally contain no resources (except for the ones required to make it a valid archive) and your application will still work as long as your classes are made available somewhere on the classpath. So, its really up to you to bundle up these classes as you see fit.

The one thing you will need is a class that extends javax.ws.rs.core.Application. This class is used as a marker/bootstrap to indicate to the application server that there are REST resources available in the web archive that need to be loaded. In most shops I've seen this class placed in the WAR project itself.

If you are really looking for organizational tips, I would recommend you start off by grouping all the REST resource classes together in their own project. Add as dependencies, any projects that are required for the resource classes to work (for example your core and ear projects). Add all three (rest, ear, core) as dependencies for your war project.

Then refine the model as needed, as you work your way through implementation.

1

There is a really awesome JBoss Maven Archetype for this sort of project.

1) Go to eclipse with maven integration installed
2) New -> Project -> Maven Project
3) Click Next
4) In Filter type in ee6
5) Out of the options that appear, use Group ID: "org.jboss.spec.archetypes", and Artifact ID: "jboss-javaee6-webapp-archetype"
6) Out will come a beautifully structured web application project with EAR connecting to a EJB project and a WAR project.