Wednesday, November 29, 2017

Easy Java EE Microservices with Payara Micro

Wondering how to get started with Java EE Microservices?  It only takes a few quick steps to deploy a Microservice using Java EE APIs.  Many think that Java EE is too heavyweight for use with Microservices, but that is simply not the case...especially if you only utilize the Java EE specifications that are required by your service.  In this brief post, I'll demonstrate how to quickly develop a Microservice with Java EE and then deploy to Payara Micro.

To download the example project, please go to GitHub:

For the purposes of this example, I will utilize NetBeans, but any Java IDE will suffice.  To get started, create a Maven web application and name it SimpleService.  Next, create two Java packages:  org.simpleservice and org.simpleservice.entity.  Once complete, the project should resemble the following figure:

Now many believe that a Microservice should not connect to an enterprise database, but I will leave that for those who wish to debate.  In this example, I will connect this service to a central Apache derby database to obtain data because I believe this to be a very likely scenario in many organizations.  In this case, we'll create a "Suggested Name" database web service, which will query a database table of suggested names for the upcoming EE4J platform.  To create the infrastructure, connect to a local Apache Derby database and create it using the following SQL:

create table SUGGESTED_NAME (
id numeric primary key,
name varchar(150));

insert into suggested_name values(1, 'Open EE');
insert into suggested_name values(2, 'Open JOE');
insert into suggested_name values(3, 'Cappucino');

Next, open the Maven POM file for the SimpleService project and add the following dependencies:


Note that there is no Java EE dependency.  This is because I am utilizing only those dependencies that are required for the service.  Each dependency is added separately.

Next, create a package org.simpleservice.entity, and create an entity class named SuggestedName within it.  For brevity, I'm not going into all of the sources here, but you can check the sources out on GitHub (

We'll need to implement our JAX-RS web service class next.  To configure the Java EE application for JAX-RS, let's create a class named ApplicationConfig and place it within the org.simpleservice package:
import java.util.Set;

 * @author Juneau
public class ApplicationConfig extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        return resources;

Next, I'll create the JAX-RS web service class itself, and I will name it SuggestedNameService.  Here are the sources for the SuggestedNameService class.  Note that I have injected a Persistence Unit.  I will get to that next.

public class SuggestedNameService {
    @PersistenceContext(unitName = "SimpleService_1.0PU")
    private EntityManager em;
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public SuggestedName find(@PathParam("id") BigDecimal id) {
        SuggestedName suggestedName = null;
        try {
            suggestedName = (SuggestedName) 
                    em.createQuery("select object(o) from SuggesetedName o " +
                    "where = :id")
                    .setParameter("id", id)
        } catch (NoResultException ex){
            System.out.println("Error: "  + ex);
        return suggestedName;
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<SuggestedName> findAll() {
        List<SuggestedName> suggestedNames = null;
        try {
            suggestedNames = em.createQuery("select object(o) from SuggestedName o")
        } catch (NoResultException ex){
            System.out.println("Error: "  + ex);
        return suggestedNames;
    protected EntityManager getEntityManager() {
        return em;

Since this service will connect to a database, I will create a persistence unit for the project.  This can be easily done in NetBeans by right-clicking the project and choosing New->Persistence->Persistence Unit.  Name the persistence unit SimpleService_1.0PU and use EclipseLink as the provider.  Do not add a data source at this point.

Once created, open the Persistence Unit and add the connection information.  In this case, I will connect to a JTA data source that I'll define next.  The data source is named DerbyDataSource, so the content of the Persistence Unit (persistence.xml) should look as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
  <persistence-unit name="AuthorService_1.0PU" transaction-type="JTA">

Create a web.xml deployment descriptor for the project.  If doing this within NetBeans, simply right-click the project and choose New->Web->"Standard Deployment Descriptor (web.xml) and click Finish.  Once the web.xml deployment descriptor has been generated, add the data source to it.

<web-app xmlns=""
That's it for the Java EE application.  You should now be able to compile the project into a WAR file and deploy to GlassFish, Payara, or another Java EE application server of your choice.  In this case, let's deploy to Payara Micro. 

To begin, download the latest Payara Micro JAR file from the website:

Once downloaded, the server can be started up by opening a command prompt and executing the JAR with your local Java runtime by typing:

java -jar payara-micro-

To deploy the application (microservice) that we've created, simply utilize the --deploy option when executing the Payara Micro JAR, and point it to the SimpleService WAR file:

java -jar payara-micro- --deploy SimpleService-1.0.war

The SimpleService microservice can now be accessed via the following URL:  http://localhost:8080/SimpleService-1.0/rest/suggestedNameService


  1. 1. Very good example for the Microprofile as well!
    2. You're defining the datasource in web.xml because it's vendor agnostic and more portable?
    3. The ApplicationConfig class has a getClasses() method. When/where do we need it?

    1. Hi Vasilis,

      Thanks for mentioning Microprofile, I should have made mention of it also! I may edit the post to mention it. Thanks to Ondro with the answer to question 3.

      As for the answer to your second question, you are correct. By placing the data source in the web.xml, we eliminate the need to customize the container at all. In this way, the service is now portable to other instances of Payara Micro or another Java EE Server.

      Hope this answers your questions. Thanks for reading!

  2. Hi Vasilis,

    I'm not Josh but I can easily answer question 3 because I also use Netbeans for development :). The answer is simple - Netbeans generates the getClasses() method when resources are created from the IDE. It's not needed provided that all resources should be enabled for the same JAX-RS root. If you want to have more roots and assign them different set of resources (very improbable) then you would have more JAX-RS applications in your web app and each would define its set of resources in the getClasses() method.

  3. Questions have been answered now, thank you both!


Please leave a comment...