Creating the source code

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

We will use the Spring Initializr project generator utility (https://start.spring.io/) in order to get a project skeleton to work on. Our microservice, as we described previously, will have to display the APIs that allow us to perform CRUD operations.

To implement our microservice, we will use the following components:

  • Web: This contains all the modules needed for full web development with a Tomcat servlet container and Spring MVC.
  • Actuator: This provides production-ready features, to help you monitor and manage your application.
  • DevTools: These are Spring Boot development tools.
  • JPA: This is the Java Persistence API, including spring-data-jpa, spring-orm, and Hibernate.
  • PostgreSQL: This is the PostgreSQL JDBC driver.

We will use com.packtpub.springboot as the Maven Group name of the project, and football-player-microservice as the Artifact. Set these values in the project form generator, as depicted in the following screenshot:

Click on Generate Project to create and download the ZIP file with the project skeleton.

Unzip the file to 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, which contains all of the dependencies needed to implement our microservice.

The project uses spring-boot-starter-parent to correctly manage dependency management, as follows:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

We will also change the value of the project name from football-player-microservice to Spring Boot Football player microservice:

<modelVersion>4.0.0</modelVersion>
<groupId>com.packtpub.springboot</groupId>
<artifactId>football-player-microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Spring Boot Football player microservice</name>
<description>Demo project for Spring Boot</description>

Before launching our first build, we need to set the database settings, in order to disable the default behaviour of Spring Boot to instantiate an in-memory database.

So, let's go into the src/main/resources directory and set this value inside the application.properties file, as follows:

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)

spring.datasource.driver-class-name=org.postgresql.Driver

spring.datasource.url=jdbc:postgresql://localhost:5532/football_players_registry

spring.datasource.username= postgres

spring.datasource.password=postgresPwd

# This property always initialize the database using sql scripts set under resources directory

spring.datasource.initialization-mode=always

# The SQL dialect makes Hibernate generate better SQL for the chosen database

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.jpa.hibernate.ddl-auto=none

Now it's time to launch the first build, with the following command:

$ mvn clean package

In this way, we will download all of the dependencies and create the Uber JAR Artifact, named football-player-microservice-0.0.1-SNAPSHOT.jar.

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

$ java -jar target/football-player-microservice-0.0.1-SNAPSHOT.jar

We can see that Spring Boot is running, and that the application is deployed:

We have not yet implemented a RESTful API; for that reason, we will use the actuator health check to verify that the application has started correctly.

Invoke http://localhost:8080/actuator/health, and check that the result is STATUS: "UP", as shown in the following screenshot:

Now let's stop the Tomcat that was created by Spring Boot, 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