Creating the source code

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

You should create a Maven project with the following dependencies:

  • JAX-RS
  • CDI
  • MongoDB Java driver
  • Arquillian
  • RESTEasy client

In this case, you won't use JPA, because version 2.0.0 of Thorntail doesn't support the use of Hibernate OGM, which allows for the use of NoSQL data stores with JPA.

Refer to the documentation at https://docs.thorntail.io/4.0.0-SNAPSHOT/#component-ogm for more details.

Remember to also set the following properties:

  • The Maven Group ID will be com.packtpub.thorntail.
  • The Maven Artifact ID will be football-team-microservice.
  • The BOM that will only include the stable fractions that are recommended for daily use.
  • The Project Name will be Thorntail Football team microservice.
  • The final name of the artifact, created in the build phase, will be football-team-microservice.
  • The HTTP port that is used by Thorntail; there are the other two microservices, which could be up and running at the same time.

Build the project, and make sure you include the WAR artifact named football-team-microservice.war and the executable Uber JAR, football-team-microservice-thorntail.jar.

At the end of this phase, you will be ready to implement the Java classes needed to reach our target.

Design and build the classes to do this, as follows:

  • A model class that simulates a JPA entity, with the following attributes:
    • String ID: The ID primary key used by the MongoDB collection
    • String name: The name of your football team
    • String city: The name of the city that usually hosts the matches of your team
    • Set <Integer> footballManagerId: The primary key of the football manager of your team, managed by the football manager microservice
    • Set <Integer> footballPlayersId: The list of the primary keys of the football players of your team, managed by the football player microservice
  • A service class that behaves like a Data Access Object (DAO), needed to manage the MongoDB CRUD operation. Here, you should implement the following operations:
    • An initialize phase, where you do the following:
      • Connect to MongoDB
      • Create the database, if it doesn't exist
      • Create the collection, if it doesn't exist
      • Delete the previous collection's values
      • Load the collection's data
    • A destroy phase, where you close the connection to the database.
    • A getTeams method that retrieves all the values.
    • A find method that retrieves the record with the primary key.
    • An edit method that modifies the record.
    • A delete method, to remove the record.
  • A RESTful web service that implements the CRUD operation with the verbs GET, POST, PUT, and DELETE.
  • A beans.xml file, needed to activate the CDI in the container.
  • A JUnit test to verify the implementations of the RESTful APIs.

OK. Now, you should be able to create your Uber JAR, with the following command:

$ mvn clean package

Launch our microservice application, as follows:

$ java -jar target/football-team-microservice-thorntail.jar

Try to invoke the API that retrieves the list of football teams, as follows:

$ curl http://localhost:8280/footballteam | json_pp

You will receive an output similar to the following:

[
{
"footballPlayersId": [
1,
4,
6,
7,
10,
12,
14,
16,
18,
19,
20
],
"city": "Turin",
"name": "Mauryno_NewTeam",
"footballManagerId": 1,
"id": "5b548e7097007545c9904ce4"
},
{
"footballPlayersId": [
2,
5,
8,
9,
11,
13,
15,
17,
21,
22,
23
],
"name": "Foogaro_Great_Lazie",
"city": "Rome",
"id": "5b548e7097007545c9904ce5",
"footballManagerId": 2
}
]

You will find the complete implementation of the microservice at the end of the chapter.

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

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