This lab will walk you through setting up a Java application using Spring Boot. At the end of this learning path you will have evolved this from a single application to a distributed application consisting of multiple microservices. In the spirit of the history of Pivotal Tracker, the suite of applications will include the microservices supporting a software project management tool, an application for tracking time, and an application for allocating people to projects.
After completing the lab, you will be able to:
gradleto run Gradle tasks
Check out the Introduction Lab.
In a terminal window,
make sure you start in the
Review the state of your Git repository:
git log --oneline --decorate --graph
You will notice that
HEAD is at second commit.
* bc868bf (HEAD -> main) added gradle wrapper to initial project * 0ac8b7f (tag: spring-boot-start, origin/main, origin/HEAD) Initial commit
You have nothing in your codebase yet except for the Gradle wrapper you generated during the Introduction lab.
Verify the state of your Git workspace:
You will notice that your local repository is ahead of your remote by one commit. That is expected:
On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean
Now that the plumbing of your application is set up, you can begin building a Spring Boot Hello World application.
pal-tracker project in your IDE.
If you are familiar with Gradle, make the following additions to
repositories closure adding Maven Central to your
and add a Java implementation dependency on the
If you are unfamiliar with Gradle at this point, or want to check
the changes that you have made, you can see what the resulting
build.gradle file should look like:
git show spring-boot-solution:build.gradle
Make sure that your
build.gradle has all of the necessary elements
before moving on.
settings.gradle file with the following contents:
rootProject.name = "pal-tracker"
This will configure the name of your Gradle project which ensures that your jar file has the correct filename.
Refresh the Gradle project in your IDE so that it picks up your changes.
Create a standard Maven directory layout.
Specifically, create a
src/main/java directory structure within
Inside of the source directory, all of your code will go into
Create this package now.
If you are creating this manually (without the help of an IDE), make sure you are creating a directory structure that mirrors the package specification:
mkdir -p src/main/java/io/pivotal/pal/tracker
Create a class in the
tracker package called
PalTrackerApplication and annotate it with
This annotation enables component scanning, auto configuration, and declares that the class is a configuration class.
main method to the
PalTrackerApplication class that will
tell Spring to run.
You can view the solution:
git show spring-boot-solution:src/main/java/io/pivotal/pal/tracker/PalTrackerApplication.java
Verify the application is set up correctly by running your application.
Using your Gradle wrapper, run the
tasks command to find which
task to use to run your application locally.
This will be the task with a description that says:
“Runs this project as a Spring Boot application”.
Once you find the task, use it to run your application.
Make sure that you run the Gradle wrapper command,
rather than the
gradle command itself.
You can do this from the command-line like this:
You may also find that your IDE provides integration to run the Gradle wrapper tasks directly.
If all is well, you will see log output from Spring Boot and a
line that says it is listening on port 8080.
Navigate to localhost:8080 and see that the
You will see a “whitelabel” error page with a status code of 404.
The application is running but it does not have any controllers.
Stop the application with
In the same package you will now create a controller class that returns
hello when the app receives a GET request at
Following labs will go in to more detail about what is happening here, but for now, just follow along.
Create a class called
WelcomeController in the
alongside the main application class .
@RestController and write a
method that returns the string
The name of the method is not important to Spring, but call it
Finally, annotate the method with
You can view the solution if you get stuck:
git show spring-boot-solution:src/main/java/io/pivotal/pal/tracker/WelcomeController.java
Verify the controller is working correctly by starting the application.
Now visit localhost:8080 to see the
Make a commit with your new changes and push your work to your repository on GitHub.
You now have a small working web application. In the next lab, you will push this application to Tanzu Application Service.
Now that you have completed the lab, you should be able to:
If you have additional time, explore the dependencies included in the
Go to the main Maven repository for
Spring Boot web starter,
find the version you are using, and navigate to its page.
You will see the Maven POM
file (in XML format).
<dependencies> section of the file shows the immediate
dependencies of the starter, for example:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.3.RELEASE</version> <scope>compile</scope> </dependency> ... </dependencies>
In Gradle syntax that corresponds to:
There is a link to the pages for each of these dependencies at the bottom of the scrolling panel at the right of the screen.
Try to write the dependencies closure in the
build.gradle file so that
your application runs without using any starters.