You are currently viewing Building a Trading Platform – Part 3: Spring Boot + MySQL
Spring Boot App

Building a Trading Platform – Part 3: Spring Boot + MySQL

We have determined that Spring Boot is a good fit for our projects data processing because of its scalability and low maintenance. The next step is now to set-up our environment for the back-end development.

 

Spring Initializr

To get us started, Spring initializr lets us generate a fully functional application on https://start.spring.io. There are a couple of Inputs we have to specify, so let us go through them:

  1. Project

    This section lets us decide between a Maven or a Gradle Project. Both are build automation tools that enable us to specify the project metadata and handle dependencies. While Gradle is faster and more powerful, Maven is easier to learn, requires less maintenance, has better IDE integrations and fulfills all our requirements. We will therefore use Maven for our project.

  2. Language

    There are 3 programming languages for us to choose from. As discussed, we will use Java for its wide usage amongst software developers and its sufficiently high performance.

  3. Spring Boot

    Similar to the above points, we have now particular requirements that would affect our choice of a specific version for now. We will go with the latest stable version at the time of writing, which is version 2.3.4.

  4. Project Metadata

    This is where it gets a bit more complicated. The properties we set in the Spring Initializr define what will be entered into the pom.xml of the project. That means we can still change any entries we make here in the future. Maven will later use the pom.xml as the basis to generate our Spring boot application. These are the elements we need to set:

    The Group will be populated with the reversed domain name. In our case, that will be “merchantvessel”. It refers to the project in a business sense. For us, that is the Trading Platform in its entirety.

    The Artifact defines the technical project.  In our project group – the Trading Platform – we have 2 “artifacts”: The Spring boot core with its API in the back-end and the Angular application in the front-end. We will therefore call our artifact “core”.

  5. The Name and Description are just free text fields describing our project. We select “Merchantvessel Core” as the name and “Java Spring Boot API for the Merchantvessel application core” as our description .

    The Java Package name is generated based on the group and artifact name. To keep things simple, we will just accept the default created by the initializr.

    For the Packaging we will choose an executable Jar

    The Java version should match the JDK version of the JAVA_HOME environment variable within our development and deployment system. If this version is not aligned with the systems version, we will get an error message when trying to run the application. Based on our JAVA_HOME variable, we choose version 11.

  6. Dependencies

    In the dependencies section, there are three elements, that are initially of interest for us:
    a) Spring Web for the API development
    b) Spring Data JPA for the persistence layer
    c) Spring Security for the JWT authentication from the front-end

Once everything is set, the initializr should look like in the screenshot:

Spring Initializr

We can now download the generated project by clicking on “Generate” and unzip it in our download folder. Using the Eclipse IDE, we can now import the project into our workspace via:
File → Import: Maven → “Existing Maven Project”.

Maven Import in Eclipse

Note that after the import, the project will not be automatically copied into the workspace of your IDE.

If the IDE shows a little exclamation mark next to the root folder of the project in the Package Explorer on the left hand side, chances are, your Java JRE and/ or JDK are not configured correctly in your IDE or are missing altogether on your operating system. In that case, your project will look similar to the screenshot:

Java_version_incompatible

To fix this, verify that your SDK and JRE are correctly installed. If you are using Windows, the path to the folder will typically look similar to this: C:\Program Files\Java\jdk1.8.0_221.
We need to make sure, that the path will point at the folder in which Java is installed, not the .exe file!

Once you have verified the successful installation or installed Java after figuring out that it was missing, verify and if necessary, set the Windows environment variable for Your Java JDK. A tutorial on how to get that done can be found here: https://mkyong.com/java/how-to-set-java_home-on-windows-10/ After your environment variable has been set, adjust the Java Version in Your Eclipse project following this article from Baeldung: https://www.baeldung.com/eclipse-change-java-version.

Assuming that your JAVA settings are now correctly configured for your project, it is time to trigger a Maven Update:  Right-click on the project root folder → Maven → Update Project … 

That will trigger an initial maven build for your application. The build can take a while because Maven will download the dependencies from the appropriate online repositories. The project has now been successfully imported into our Eclipse IDE, though we are not quite ready yet! Let us try to execute the project: Right-click on the project root folder Run As → Java Application. The Spring boot application should start now, download a few sources, and then run into an error.

Connect Spring Boot to MySQL

If you remember, when we configured the dependencies for our Spring Boot application with the Spring Initializr, we added “Spring Data JPA”.

Upon startup, Spring will try to find the correct database to connect to. Since we have neither setup a database nor configured a database connection, our application has to crash. Step one will therefore be to obtain a local MySQL instance. This can easily be done following tutorials like this: https://dev.mysql.com/doc/refman/8.0/en/windows-installation.html

Now open your MySQL workbench and ensure that the server is running and that you have the correct credentials for the database connection. On your development instance, that can be simply be the root user. Then create a new schema in your instance. In MySQL, a schema is effectively the database you will connect to. In our case, that will be “merchantvessel_core”. Not that the name should be all lower caps and words should be separated with an underscore.

These credentials, together with the schema name will be used to configure our database connection. Open the application.properties file in the resources folder of your project:

application properties default folder structure

Here, you define the following properties:

spring.profiles.active=@activatedProperties@
spring.jpa.database=MYSQL
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=create

The first property enables to use of different profiles for different environments. Each “profile” is a separate file, following the convention application-.properties In our case, we will generate 2 profiles, by creating two corresponding files:

  1. Development: application-dev.properties
  2. Production: application-prod.properties

The development profile will contain the following content:

# Database Settings
spring.datasource.url=jdbc:mysql://localhost:3306/brownbag
spring.datasource.username=root
spring.datasource.password=HansaShift2020
server.port=8081

The properties are generally self-explanatory, so they shall not be further discussed here. As for the spring.datasource.url however, there are two common issues, which may arise:

  1. A time zone incompatibility.
  2. An SSL handshake failure

In that case, add the appropriate parts of the following parameters to the database connection string:

?useTimezone=true&serverTimezone=UTC&useSSL=false

Of course, while circumventing the use of SSL is usually save in a development scenario, things are very different in a production environment. In that case, make sure to investigate the underlying issue!

In a productive environment, the settings that we have so far defined in our development properties file, will be injected by our hosting environment. The file can therefore remain empty for now. The updated project structure should now look like this:

Application Properties
Each spring profile gets its own application.properties file

Now that we have specified the connection details to our MySQL database, it is time to specify a JDBC driver in our dependencies. To achieve this, we add the MySQL dependency from https://mvnrepository.com/artifact/mysql/mysql-connector-java into our pom.xml file in the project root folder, by adding the following expression in the dependencies section:

Below the </dependencies> tag in the pom.xml, we describe the profiles that we want Spring Boot to consider. With the attribute <activeByDefault>, we advise our IDE to use the dev profile, unless otherwise specified:

If we try to execute our application again via: Right-click on the project root folder  Run As  Java Application, it should start-up without issues.

Congratulations! You have just successfully created Your first Spring Boot application!

Leave a Reply