Jakarta EE 9 Hello World example application

In the last post we had our first taste of Jakarta EE 9 with the preview version of WildFly 22: How to run Jakarta EE 9 on WildFly

Let’s see now how to build and deploy sample application which uses the ‘jakarta‘ package namespace.

The jakarta.jakartaee-api version 9 is now available on the official Maven repository: https://search.maven.org/search?q=g:jakarta.platform

This means that you can now build your applications using ‘jakarta‘ packages as follows:

<dependency>
    <groupId>jakarta.platform</groupId>
    <artifactId>jakarta.jakartaee-api</artifactId>
    <version>9.0.0</version>
    <scope>provided</scope>
</dependency>

If you just need the Web Profile capabilities, then you can use instead:

<dependency>
    <groupId>jakarta.platform</groupId>
    <artifactId>jakarta.jakartaee-web-api</artifactId>
    <version>9.0.0</version>
    <scope>provided</scope>
</dependency>

Let’s rewrite a WildFly quickstart to use the new packaging. Here’s an Hello World Servlet:

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.inject.Inject;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/HelloWorld")
public class HelloWorldServlet extends HttpServlet {

    static String PAGE_HEADER = "<html><head><title>helloworld</title></head><body>";
    static String PAGE_FOOTER = "</body></html>";

    @Inject
    HelloService ejb;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();
        writer.println(PAGE_HEADER);

        try {
            writer.println(ejb.createHelloMessage("Jakarta EE 9!"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        writer.println(PAGE_FOOTER);
        writer.close();
    }

}

And here’s the HelloService ejb:

import jakarta.ejb.Stateless;
 
@Stateless
public class HelloService {

  public String  createHelloMessage(String name) throws Exception {
         return "Hello "+name;
         
  }
  
}

Now grab WildFly 22 from https://www.wildfly.org/downloads/ and start it:

$ ./standalone.sh

Build and deploy the application on it:

$ mvn install wildfly:deploy

You will see from the logs that the application deployed on WildFly:

11:46:23,735 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) WFLYSRV0027: Starting deployment of "helloworld.war" (runtime-name: "helloworld.war")
11:46:23,798 INFO  [org.jboss.weld.deployer] (MSC service thread 1-8) WFLYWELD0003: Processing weld deployment helloworld.war
11:46:23,820 INFO  [org.jboss.as.ejb3.deployment] (MSC service thread 1-8) WFLYEJB0473: JNDI bindings for session bean named 'HelloService' in deployment unit 'deployment "helloworld.war"' are as follows:

	java:global/helloworld/HelloService!org.jboss.as.quickstarts.helloworld.HelloService
	java:app/helloworld/HelloService!org.jboss.as.quickstarts.helloworld.HelloService
	java:module/HelloService!org.jboss.as.quickstarts.helloworld.HelloService
	java:global/helloworld/HelloService
	java:app/helloworld/HelloService
	java:module/HelloService
11:46:24,136 INFO  [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0010: Deployed "helloworld.war" (runtime-name : "helloworld.war")

Test it:

Jakarta ee 9 hello world

That was our first ride on Jakarta EE 9 with WildFly. As said in our first article, at the moment using the ‘jakarta‘ packaging is not mandatory as the transformer capabilities of wildfly-preview layer are able to perform the required bytecode changes in the package name, so everything still works with the former ‘javax’ packaging. Enjoy Jakarta EE 9!

Source code for this tutorial: https://github.com/fmarchioni/mastertheboss/tree/master/jakartaee/jakartaee-9