Develop, build e and run Java-EE-8-powered microservices

Learn how to work with Java EE 8 microservices in this guest post by Mario-Leander Reamer, a senior Java developer and the author of Building RESTful Microservices with Java EE 8.

In this section, you’re going to take a look at the following things:

  • How to develop, build, and run your first Java-EE-8-powered microservices
  • Required Java EE 8 dependencies for basic web-service development
  • Basic components of any JAX-RS-based web service
  • Deployment of a thin WAR artifact using Payara Server 5

Time to get started and dive into the code. The following is a prepared IDE and a raw skeleton Maven project. What you see here is a very basic POM file:

There’s one thing missing though; first, you need to define the required dependency for the Java EE 8 API:


Specify version as 8.0. You must also define the proper scope for this, which, in this case, is provided, because the Java EE 8 API will later be provided by the application server. Next, add a beans.xml descriptor to the WEB-INF directory of the web application:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

Next, you need to bootstrap your JAX-RS application. Create a class called JAXRSConfiguration. The name really doesn’t matter. What’s important is that this class extends from the Application base class. Bear in mind the package while selecting Application. It’s also important that you specify the @ApplicationPath annotation. This will be the base path to access the REST API:

package com.packtpub.javaee8;

//skipping imports to save space

 * Configures a JAX-RS endpoint.
public class JAXRSConfiguration extends Application {

Once you’ve bootstrapped JAX-RS, what’s missing is a proper REST resource. Create a class called HelloWorldResouce. You’ve used the @Path annotation, which will be the path this resource will be accessible under. Call that “hello”. Next up, create a method that will produce the proper response once called: helloWorld. Use the proper Response here; annotate this using the @GET annotation, as you’ll need to issue GET requests later on, and say that it produces MediaType.APPLICATION_JSON. Then return Response.ok, where ok is HTTP status 200 of a response when you call the build. So, what should be used as the response? Use Map<String, String> as the response and return singletonMap with the message key and the Hello World value:

package com.packtpub.javaee8;

//skipping imports to save space
* The REST resource implementation class.
public class HelloWorldResource {
   public Response helloWorld() {
      Map<String, String> response = singletonMap("message", 
                     "Building Web Services with Java EE 8.");
      return Response.ok(response).build();

You should already have a basic working microservice. Now deploy this onto our Payara Server 5 and run it. You’re going to deploy the WAR file that’s been built; you can see that it’s already been deployed and the deployment took 5.1 milliseconds.

Check your browser. You should see the “Hello World.” message, as shown in the following screenshot:

Now modify the value here to “Building Web Services with Java EE 8”. Deploy this once more and update your artifact. The new version has been deployed. Go back to your browser to check whether you have the proper response message:

That’s all for this section; in the next section, you’ll see how to containerize a Java EE 8 microservice.

Containerizing Java EE 8 microservices

In this section, you’re going to take a look at how to containerize and run Java EE 8 microservices using Docker. You’ll learn how to write a basic Dockerfile and see how to build and run the Docker image using Payara Server full and Payara Server micro edition. Open the IDE to the microservice project from the previous section. All that’s missing here is Dockerfile, so create one.

You must be wondering about which base image you should use. You have two basic options when using Payara—you can either use the server-full image or the Payara micro edition. For the purposes of this tutorial, use the full version of Payara first. Dockerfile will be as follows:

FROM payara/server-full:5-SNAPSHOT
COPY target/hello-javaee8.war $DEPLOY_DIR

In the preceding Dockerfile, you’ve mentioned that you’re using payara/server-full. You need to use its correct version, which, in this case, is version 5-SNAPSHOT. Then, you can copy the hello-javaee8.war file of your microservice into the correct location of the produced image. You need to issue a COPY command from target/hello-javaee8.war and then copy this into the deployment directory. To check whether it works, open a console, keeping in mind that you’re in the right directory. Check whether everything is packaged nicely by calling mvn package, just to make sure the WAR file is in the correct state. If it is, you’ll see things running while compiling, an absence of tests, and an updated WAR file:

Build Docker using -t, which specifies the tag you want to use, by calling hello-javaee8 and giving it version number 1.0:

>docker build -t hello-javaee8:1.0 .

Use the following command to see whether your server starts up:

>docker run -it -p 8080:8080 hello-javaee8:1.0

You’ve mapped port 8080 from the container onto your Docker host. You’ll see that the Payara Server is starting up in the console (which would take only a couple of seconds), after which the application will be deployed. To check whether you can reach the web service, hit the IP address as shown in the following screenshot:

Now it’s time to see how to use the Payara micro edition. Stop the service you just created and delete the contents of Dockerfile. First, you need to change FROM by use a different base tag for this image (payara/micro:5-SNAPSHOT). Then, copy the hello-javaee8.war file into the proper location for this base image. Next, copy the WAR file in the target directory and call it to /opt/payara/deployments. This is the default deployments directory for the micro edition base container. The Dockerfile should look as follows:

FROM payara/micro:5-SNAPSHOT
COPY target/hello-javaee8.war /opt/payara/deployments

Switch back to the console and issue the Docker build command again:

>docker build -t hello-javaee8:1.0 .

Fire up the container again:

>docker run -it -p 8080:8080 hello-javaee8:1.0

You can see that the output in the console changes, as you’re using the Payara micro runtime this time. This takes a couple of seconds to spin up the web service. You can see that the REST Endpoints are available. To check again, go to the management console; you can see that you have a running container. Try calling the web service from the browser, as shown in the following screenshot:

As you can see, everything’s working just fine and you have a running dockerized version of the service.

You’ve learned the basics of working with Java EE 8 microservices. To learn more about RESTful web services and Java EE 8, you can explore Building RESTful Microservices with Java EE 8. It’s a comprehensive guide that shows you how to use the APIs provided by Java EE to create RESTful web services. The book goes in depth to explain the usage of different JAX-RS APIs, while also delving into the structure of web services and data sharing between applications. The book is a must-read for Java developers keen on learning how to implement web services using the latest Java EE 8 APIs.


Leave a Reply

Your email address will not be published. Required fields are marked *