As most of you probably know, the Java EE was migrated from Oracle to the Eclipse Foundation, and it is now called Jakarta EE, under the Eclipse Enterprise for Java (EE4J) project. There are already a list of application servers which offer a Jakarta EE 8 compatible implementation such as WildFly 18. In this tutorial we will learn how to bootstrap a Jakarta EE project.
In general terms, Jakarta EE 8 and Java EE 8 APIs are identical and so is their implementation. In practical terms, even if the source from which the server is built changes, it is not expected to introduce any runtime incompatibility. Therefore, WildFly 18 is still a Java EE 8 compatible application server.
Let’s start from the configuration. We will go through the Maven and Gradle configuration of a Jakarta EE project.
Configuring Jakarta EE with Maven
If you are using Maven to build your Jakarta EE project, then you have two main options:
1) Use the “Vanilla” Jakarta EE dependency system:
<dependencies> <dependency> <groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-api</artifactId> <version>8.0.0</version> <scope>provided</scope> </dependency> </dependencies>
This is the simplest choice if you stick to standard Jakarta EE API or if you want to quick Test a Jakarta EE application.
2) Use WildFly (or other compatible Jakarta EE server) dependency system:
<dependencyManagement> <dependencies> <dependency> <groupId>org.wildfly.bom</groupId> <artifactId>wildfly-jakartaee8-with-tools</artifactId> <version>${version.server.bom}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>jakarta.enterprise</groupId> <artifactId>jakarta.enterprise.cdi-api</artifactId> <scope>provided</scope> </dependency> <!-- Other Dependencies here --> </dependencies>
This options pays back if you are using WildFly features that extend the Jakarta EE umbrella. For example, the wildfly-jakartaee8-with-tools BOM includes also the correct versions for Arquillian, so you don’t need to manage their version. Besides it, WildFly extends the Jakarta EE project including a growing subset of the Eclipse MicroProfile API. So, summing up, if you are using features that extend Jakarta EE, it is worthy to use WildFly BOM and dependencies to build your projects.
Configuring Jakarta EE with Gradle
If you are using Gradle to bootstrap your projects, then the recommended gradle.build you can use is the following one, which creates a sample jakartaee-demo.war application under the base package com.mastertheboss:
apply plugin: 'war' group = 'com.mastertheboss' version = '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { providedCompile 'jakarta.platform:jakarta.jakartaee-api:8.0.0' } compileJava { targetCompatibility = '11' sourceCompatibility = '11' } war{ archiveName 'jakartaee-demo.war' }
A sample Jakarta EE 8 project
To show you an example, we have moved one Hello World Rest/JPA Example coded for Java EE 8 into the new Jakarta EE 8 style.
This project is made up of a basic REST Endpoint that exposes a set of methods to list and create resources:
@Path("/service") public class RESTService { @Inject ServiceBean ejb; @GET @Path("/list/{id}") @Produces(MediaType.APPLICATION_JSON) public SimpleProperty getPropertyByPathParam(@PathParam("id") String id) { return ejb.findById(id); } @GET @Path("/list") @Produces(MediaType.APPLICATION_JSON) public List<SimpleProperty> getProperty() { return ejb.findAll(); } @POST @Produces(MediaType.TEXT_PLAIN) public Response createProperty(@FormParam("key")String key, @FormParam("value")String value) { ejb.put(key,value); return Response.ok("Inserted! Go back and check the list.").build(); } }
This is the ServiceBean which does some queries on the default Database:
@Stateless public class ServiceBean { @PersistenceContext private EntityManager em; public void put(String key, String value){ SimpleProperty p = new SimpleProperty(); p.setKey(key); p.setValue(value); em.persist(p); } public void delete(SimpleProperty p){ Query query = em.createQuery("delete FROM SimpleProperty p where p.key='"+p.getKey()+"'"); query.executeUpdate(); } public List<SimpleProperty> findAll(){ Query query = em.createQuery("FROM SimpleProperty"); List <SimpleProperty> list = query.getResultList(); return list; } public SimpleProperty findById(String id){ SimpleProperty p = em.find(SimpleProperty.class, id); return p; } }
To build up this project, we have used the standard Jakarta EE 8 dependencies:
<dependencies> <dependency> <groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-api</artifactId> <version>8.0.0</version> <scope>provided</scope> </dependency> </dependencies>
You can find the full source code for the project here: https://github.com/fmarchioni/mastertheboss/tree/master/jakartaee/standard
As the project includes also WildFly Maven plugin, you can simply run it as follows:
$ mvn install wildfly:deploy
Here is your first, Jakarta EE 8 Project running on WildFly:
Have fun with Jakarta EE!