RESTful web service – JAX-RS and CDI

Now, it's time to build our RESTful web service implementation, as follows:

  1. Delete the default implementation class, HelloWorldEndpoint.java, created by the Thorntail generator.
  2. Define a new package, named service (the fully qualified name will be com.packtpub.thorntail.footballmanagermicroservice.rest.service), where we will put the RESTful web service implementation.
  3. Build the API implementations by using the CDI specifications (to inject the entity manager used to manage the database connections) and JAX-RS (to implement the CRUD RESTful operations). You can follow the same steps that were implemented in the The football player microservice section.
  4. Remember to set the @Path of your RESTful implementation to /footballmanager.
  5. Before we create our Uber JAR and run the application, we must change the port that the Thorntail instance will use. You must remember that there is the football player microservice that runs on the default 8080 port.
  6. To avoid port conflict, we will set the following property in the Maven pom.xml file:
...
<configuration>
<properties>
<swarm.port.offset>100</swarm.port.offset>
</properties>
</configuration>
...
  1. Create our Uber JAR using the following command:
$ mvn clean   package  
  1. Then, launch our microservice application with the following command:
$ java -jar target/football-manager-microservice-thorntail.jar
  1. Now, we can invoke the API that retrieves the list of football managers, using the following command:
$ curl http://localhost:8080/footballmanager | json_pp
  1. Finally, we will create the JUnit test, in order to ensure that our APIs work properly. Let's add the Maven dependencies, as follows:
<properties>
...
<version.resteasy>3.0.19.Final</version.resteasy>
</properties>

<dependency>
<groupId>io.thorntail</groupId>
<artifactId>arquillian</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${version.resteasy}</version>
<scope>test</scope>
</dependency>
  1. Then, we will create the test class, named FootballManagerFacadeRESTTest, in the package com.packtpub.thorntail.footballmanagermicroservice.rest.service, under the src/main/test directory.

Follow the same steps that were described in the Entity class – JPA section, and try to do it yourself. If you can't, don't worry; at the end of the section, I will give you a link to the GitHub repository to find the solution.

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

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