Creating the source code

We have installed and configured everything that's necessary to create our microservice for the management of the registry of players. Now, it's time to write the code that's needed to expose our microservice APIs.

We will use the Thorntail project generator utility, https://thorntail.io/generator/, in order to get a project skeleton to work on. Our microservice, as described previously, will have to display APIs that allow us to perform CRUD operations. Java EE and Jakarta EE have specifications for implementing these features, as follows:

  • JAX-RS
  • CDI
  • JPA
  • JTA

We will use com.packtpub.thorntail as the Maven Group ID of the project, and football-player-microservice as the Artifact ID:

  1. Set the Group ID, Artifact ID, and Dependencies values in the project form generator, as shown in the following screenshot:

  1. Click on Generate Project to create and download the ZIP file with the project skeleton.
  1. Unzip the file in a directory of your choice, and open the Maven project with your favorite IDE (Eclipse, NetBeans, IntelliJ, and so on).

The core element of the project is the Maven pom.xml file that contains all of the dependencies needed to implement our microservice.

The project uses the BOM to correctly manage dependency management, as follows:

<properties>
...
<version.thorntail>2.0.0.Final</version.thorntail>
...
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>bom-all</artifactId>
<version>${version.thorntail}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
  1. The default value is set to bom-all, which includes all of the fractions—stable, unstable, experimental, and even deprecated. We need to change it to bom, which only includes the stable fractions; it's recommended for daily use:
<properties>
...
<version.thorntail>2.0.0.Final</version.thorntail>
...
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>bom</artifactId>
<version>${version.thorntail}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
  1. We will also change the value of the project name from Thorntail Example to Thorntail Football player microservice:
...
<modelVersion>4.0.0</modelVersion>
<groupId>com.packtpub.thorntail</groupId>
<artifactId>football-player-microservice</artifactId>
<name>Thorntail Football player microservice</name>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
...
  1. We will change the final name of the artifact, created in the build phase, from demo to football-player-microservice, as follows:
<build>
<finalName>football-player-microservice</finalName>
...
</build>
  1. Now, launch the first build with the following command:
$ mvn clean   package 

In this way, we will download all of the dependencies and create the WAR artifact named football-player-microservice.war and the super-executable JAR file, football-player-microservice-thorntail.jar.

To check that the project is ready to be used, we can run it by using the following command:

$ mvn thorntail:run  

First of all, you will notice that all of the fractions related to JAX-RS, CDI, and JPA are installed and running, as follows:

2018-07-14   16:38:50,356 INFO  [org.wildfly.swarm] (main) WFSWARM0013: Installed   fraction:                      JAX-RS - STABLE         io.thorntail:jaxrs:2.0.0.Final
2018-07-14 16:38:50,363 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: Logging - STABLE io.thorntail:logging:2.0.0.Final
2018-07-14 16:38:50,363 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: Undertow - STABLE io.thorntail:undertow:2.0.0.Final
2018-07-14 16:38:50,364 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: Elytron - STABLE io.thorntail:elytron:2.0.0.Final
2018-07-14 16:38:50,364 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: CDI - STABLE io.thorntail:cdi:2.0.0.Final
2018-07-14 16:38:50,364 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: CDI Configuration - STABLE io.thorntail:cdi-config:2.0.0.Final
2018-07-14 16:38:50,364 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: Bean Validation - STABLE io.thorntail:bean-validation:2.0.0.Final
2018-07-14 16:38:50,365 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: Transactions - STABLE io.thorntail:transactions:2.0.0.Final
2018-07-14 16:38:50,365 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: JPA - STABLE io.thorntail:jpa:2.0.0.Final
2018-07-14 16:38:50,365 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: Datasources - STABLE io.thorntail:datasources:2.0.0.Final
2018-07-14 16:38:50,365 INFO [org.wildfly.swarm] (main) WFSWARM0013: Installed fraction: JCA - STABLE io.thorntail:jca:2.0.0.Final

Then, you can see that Thorntail is running, and that the application is deployed:

2018-07-14   16:38:55,555 INFO  [org.wildfly.extension.undertow] (ServerService Thread   Pool -- 10) WFLYUT0021: Registered web context: '/' for server 'default-server'
2018-07-14 16:38:55,598 INFO [org.jboss.as.server] (main) WFLYSRV0010: Deployed "football-player-microservice.war" (runtime-name : "football-player-microservice.war")
2018-07-14 16:38:55,607 INFO [org.wildfly.swarm] (main) WFSWARM99999: Thorntail is Ready

Finally, we can invoke the application at http://localhost:8080/hello, and see the message, Hello from Thorntail!. Now, let's stop Thorntail by using the Ctrl C command, and start to update our project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset