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.
Learning outcomes
After completing the lab, you will be able to:
- Describe how to create runnable Spring Boot application
- Describe how to create a controller that responds to HTTP requests
- Use
gradleto run Gradle tasks
Getting started
-
Check out the
Introduction Lab. -
In a terminal window,
make sure you start in the~/workspace/pal-trackerdirectory. -
Review the state of your Git repository:
git log --oneline --decorate --graphYou will notice that
HEADis at second commit.* bc868bf (HEAD -> main) added gradle wrapper to initial project * 0ac8b7f (tag: spring-boot-start, origin/main, origin/HEAD) Initial commitYou have nothing in your codebase yet except for the Gradle wrapper
you generated during the
Introduction lab. -
Verify the state of your Git workspace:
git statusYou 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
If you get stuck
If you get stuck during this lab,
you can either
view the solution,
or you can
fast-forward to the spring-boot-solution tag.
Bootstrap the application
Now that the plumbing of your application is set up,
you can begin building a Spring Boot Hello World application.
-
Import the
pal-trackerproject in your IDE. -
If you are familiar with Gradle, make the following additions to
yourbuild.gradlefile:-
Add a
pluginsclosure:-
Apply the
2.5.3.RELEASEversion of the
Spring Boot Gradle plugin. -
Apply the
1.0.11.RELEASEversion of the
Spring Dependency Management plugin -
Apply the
Java plugin.
-
-
Create a
repositoriesclosure adding Maven Central to your
build.gradlefile. -
Add a
dependenciesclosure,
and add a Java implementation dependency on the
org.springframework.boot:spring-boot-starter-webpackage.
-
-
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.gradlefile should look like:git show spring-boot-solution:build.gradleMake sure that your
build.gradlehas all of the necessary elements
before moving on. -
Add a
settings.gradlefile 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/javadirectory structure within
thepal-trackerdirectory. -
Inside of the source directory, all of your code will go into
theio.pivotal.pal.trackerpackage.
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
trackerpackage called
PalTrackerApplicationand annotate it with
@SpringBootApplication.This annotation enables component scanning, auto configuration, and
declares that the class is a configuration class. -
Add a
mainmethod to thePalTrackerApplicationclass that will
tell Spring to run.This
mainmethod executes the Spring Boot
SpringApplication.run
method which bootstraps the
Dependency Injection
container, scans the classpath for
beans,
and starts the application.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
taskscommand 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,
gradlew,
rather than thegradlecommand itself.
You can do this from the command-line like this:./gradlew tasksYou 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
application responds.
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 withCtrl+C.
Create a controller
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
WelcomeControllerin thetrackerpackage,
alongside the main application class . -
Annotate
WelcomeControllerwith@RestControllerand write a
method that returns the stringhello.The name of the method is not important to Spring, but call it
sayHello.
Finally, annotate the method with@GetMapping("/").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
hello
message. -
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.
Wrap up
Now that you have completed the lab, you should be able to:
- Describe how to create runnable Spring Boot application
- Describe how to create a controller that responds to HTTP requests
- Use the Gradle wrapper to run Gradle tasks
Extra
If you have additional time, explore the dependencies included in the
spring-boot-starter-web library.
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).
The <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:
implementation 'org.springframework.boot:spring-boot-starter:2.5.3.RELEASE'
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.