Building Your First Spring Boot Web Application
Want to learn how to build your first Spring Boot web application using Maven and a web application controller? Check out this tutorial to learn how with Spring Boot!
Join the DZone community and get the full member experience.
Join For FreeIn my previous article, I wrote about the Spring Boot fundamentals. Now, its time to show you a Spring Boot web application example. In this module, you will learn how to create a simple Spring Boot application and what dependencies and technologies do you need to get you started.
Furthermore, we will go a little deeper into the basic fundamentals. I will explain some of the most crucial behind the scenes working mechanisms, which you need to understand if you would like to be a professional developer.
How to Build Your First Spring Boot Web Application
Prerequisites
To create a new Spring Boot application, we will use the following in our example:
You can simply download the provided links above and install them.
If you not familiar with them:
- Spring STS: The Spring Tool Suite (STS) is an Eclipse-based development environment that is customized for developing Spring applications.
- Maven: A tool that you can use for building and managing any Java-based project.
You do not need to know much about them for now, we will just use their basic functionality.
In one of my upcoming articles, we will talk about Spring STS and Maven in greater detail.
Creating Your Project
After you started your STS, right-click the package explorer and select New —> Maven project.
Then, we can use our default Workspace location.
Select the default maven-archetype-quickstart artifact.
Give any name for your Spring Boot application.
Set up Your pom.xml
After you clicked the finish button, open the pom.xml and add the spring-boot-starter-parent and the spring-boot-starter-web dependency as below:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot</groupId>
<artifactId>my-first-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-first-application</name>
<url>http://maven.apache.org</url>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Here, we use the Spring Boot version v 2.0.3, but you can give any valid version that you would like.
Configure Your Application as a Spring Boot Application
After that seek for your App.java class, the Maven quickstart archetype created it for us. This is where our public static void main( String[] args
)method is located, which is the entry point of all Java applications. It should be in our root package. You will need to open it and add the @SpringBootApplication
annotation to the class and the SpringApplication.run(App.class, args)
row to our main method.
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
Set up Your Web Application Controller
If you all have done this, create a new package called controller
. Add a new class for this package and update the following:
@RestController
public class TestController {
@RequestMapping("/")
public String home() {
return "Spring boot is working!";
}
}
The class and method name does not matter — you can call them anything.
As you can see, we added the @
RestController
for the class, and we created a method with the @RequestMapping
annotation. These are the part of the Spring MVC framework. We will talk about that in greater detail later.
Start Your Web Application
So, that's it. Now, we can start our newly created Spring Boot web application as a simple Java application:
Right click on our App.java class -> Run As -> Java Application. After this, you should see something similar in the Console log:
- 2018-08-06 20:22:53.221 INFO 9152 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
- 2018-08-06 20:22:53.225 INFO 9152 --- [ main] springboot.my_first_application.App : Started App in 1.862 seconds (JVM running for 2.419)
Here, you can see that our application has been started. Furthermore, we have a running Tomcat web server, which registered to our 8080 port.
Try Out Your Web Application
To have a look at whether our application is it working or not, we can simply open a web browser and type: http://localhost:8080/. As we saw above, 8080 was the port where our web server is registered.
You should see the following:
How Does it Work Behind the Scenes?
Dependency Management
After you added your starting dependencies, Maven downloaded all of the libraries considered to be necessary for our project.
You can check this by opening the pom.xml and clicking on the dependency hierarchy tab shown below:
It knows which dependency versions are the latest and stable for each other. This makes our life easier, because you do not need to take care of the different transitive dependencies. You only need to add one starter dependency, and your greatest dependencies start to download automatically.
The spring-boot-starter-parent
that is added to the starter section tells the default version of our Spring Boot which one we would like to use. Thus, we do not need to give version numbers later on, because every other one is going to align like this.
How Does a Spring Boot Application Start?
- Firstly, the application starts with a simple Java public static main method.
- Next, Spring starts your Spring context by looking up the auto-config initializers, configurations, and annotations that direct how to initialize and start up the Spring context.
- Lastly, it starts and auto-configures an embedded web server. The default application server is Tomcat for Spring Boot.
Benefits of Containerless Deployment
As you can see here, we didn’t need to configure our web-container. Therefore, we gain a lot of time and could get rid of some of our configuration files.
The reason that Spring Boot runs an embedded web-server and configures it itself is that it knows that we need it from the web dependency, which we added to our pom.xml.
Spring Boot opens up a bunch of new opportunities for us— we can simply run a web app by copying a basic .jar
file anywhere Java is installed and just run it. This is a big step towards cloud architecture because we can handle our independent deployments more easily than before.
Summary
In conclusion, you just learned how to build up a very basic Spring Boot application from scratch. Now, you know how to add your dependencies and how they resolve. Furthermore, we talked about how your web application starts and the benefits of moving towards containerless deployments.
Special thanks to Bunker Dan for his high-quality video training where I learned a lot of this information.
Published at DZone with permission of Zoltan Raffai, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments