Simple introduction to Spring Boot - Part 2

In this part I will show how to create a new Spring Boot 3 project and create a Java entity class to model a pet.

This part assumes you have completed Part 1 of this series of articles, where we verified that when creating a new Maven project in NetBeans we could see the option to create a new Spring Boot Initializr project:

Ensure Spring Boot Initializr project is selected from the Java with Maven category and click the Next > button.

This will give you the following dialogue that you should fill in as shown:

  • The Group and Artifact together form the Package Name to make the classes in the application relevant to our own organisation
  • The Name and Description are set to something sensible to describe the application
  • While I have selected Java version 17, you can choose any of the other options as we aren't making use in this guide of any of the newer Java constructs. You might need to select a different Java version if you know you only have that available.

When you have filled in the input fields click Next >.

The next section will let you select the Maven dependencies:

  • Under Developer Tools select Spring Boot DevTools. This will allow it to automatically reload itself whenever you change the software, which is a great time saver during development
  • Under Web select Spring Web. This provides the core facilities for developing web applications
  • Under Template Engines select Thymeleaf. This allows us to include coding within our HTML files to integrate with our Java objects
  • Under SQL select Spring Data JPA. This allows us to integrate our Java objects with a database
  • Also under SQL select H2 Database. This is a simple in-memory relational database that is easy to configure and therefore simplifies development

Click the Next > button, and then fill in the resulting dialogue as shown below:

You can choose a different Project Location if you wish.
Ensure the two check boxes are selected.

Now click Finish and wait a few moments for NetBeans to generate the code file to get us started. Once ready, the screen will look as follows:

You may notice at the bottom of the page it says Downloading Maven dependencies. This may take a few minutes to complete.

You initially see the pom.xml file that configures the Maven build-tool with the dependencies needed. If you scroll through these you will see how they correlate with the selected dependencies in a previous step. For example, the artifactId spring-boot-starter-data-jpa corresponds with the Spring Data JPA selection.

This means if you need to add any further dependencies, such as for security, then you just need to add another <dependency> section in this XML file and Maven will take care of the necessary downloads and compatibilities.

For the time being you can close the pom.xml file tab if you wish.

Under the top-left section headed Projects, click the arrowhead next to Pet System to expand it, and then expand Source Packages followed by com.example.petsystem so that it looks like this:

Double-click PetSystemApplication.java to open it. This shows the following:

This shows the initial class with a main(String [] args) method that initiates the application. We won't need to change this for our tutorial so you can just close it again.

In the Projects window expand Other Sources followed by src/main/resources. Now right-click on templates and select New --> Other...

In the next dialogue choose Other under Categories and then select HTML file under File Types

Click Next > and enter index.html for the File name, and then click the Finish button.

This opens up the index.html file that will be our web application's home page:

Edit this index.html file so it contains the following code to serve as the basis of our simple home page to list our pets:

<!DOCTYPE html>
<html>
    <head>
        <title>Pet System</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>Pet System</h1>
        <h2>List of pets</h2>
        <table border="1">
            <tr>
                <th>Name</th>
                <th>Date of Birth</th>
            </tr>
            <!-- rows of pets from database will go here -->
        </table>
        <br/>
        <a href="/addpet">Add pet</a>
    </body>
</html> 

The home page comprises a simple table of two columns, for the name of each pet and its date of birth. I will revisit this home page later to show how to connect it to Java objects.


In Part 3 I will focus on setting up the Java classes to model the pets and how they can be stored within a database table.

Simple introduction to Spring Boot - Part 3


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Monday, 27 October 2025

Captcha Image