Overview about Spring Boot

Spring Boot is a member of the big Spring family. It helps the programmers simplify the process of programming an application with Spring, focused solely on business development for the application. So how is it in details? In this tutorial, let’s learn together.

First of all, assume that you need to develop a basic HelloWorld web application using the Spring framework. These application programming stages will at least include the following:

  • Create a project using Maven with the necessary dependencies of Spring MVC and Servlet API.
  • A web.xml file to declare the Spring MVC DispatcherServlet.
  • A Spring MVC configuration file.
  • A Controller class returns a “Hello World” page when a request arrives.
  • Finally, there must be a web server to deploy the application to run.

In total, we have at least 5 stages, right?

In these steps, if you are aware, you will see that only one process of creating a Controller class can be different for different applications because each application has a different requirement. As for the other steps, we all need to develop an application with Spring and they may be the same.

To reduce the likelihood of this, Spring Boot has been introduced. Spring Boot will automate all of the same possible steps; all we need to do is: develop the application code relate to the business of the application, mainly with Spring’s Controller.

To illustrate what Spring Boot does, in this tutorial, I will use the Spring Boot Command Line Interface (CLI) to run an example. By that, you will find that, just a few small operations, we were able to create an application that runs with Spring Boot.

OK, let’s get started.

First, let’s install the Spring Boot CLI first. If someone does not know how to install, you guys can refer to here.

My Spring Boot CLI is as follows:

Overview about Spring Boot

Now, I will use the Spring Boot CLI to create a basic web application using the following command:

with:

  • -d = web means we are going to use spring-boot-starter-web
  • -g is Maven Group Id
  • -a is Maven Artifact Id
  • –package-name is the name of the package.

Here, I will create a web application named hello as follows:

Overview about Spring Boot

As a result, a folder named hello will be created along with the structure of a Maven project as follows:

Overview about Spring Boot

Use the command line to navigate to the hello directory and use the following command:

to run the web application we just created.

Result:

Overview about Spring Boot

As you can see, Spring Boot automatically does everything to run our web application on Tomcat with the default port of 8080.

If you now use a browser and access http://localhost:8080 then the result will look like this:

Overview about Spring Boot

We are seeing a 404 Not Found error, which is because in the project we just created, there is no Controller that handles the request to the homepage.

Now, if you create a HelloController class with the following content:

and placed in the /src/main/java/com/huongdanjava/springboot directory:

Overview about Spring Boot

then run the command again:

The results are as follows:

Overview about Spring Boot

Obviously, as you can see, we do not spend much effort creating a web project that can run. Everything Spring Boot has done automatically, which we are interested in is just Controller.

 

Add Comment