Spring Boot Simple Application
In last topic, we have seen that there are various ways for creating Spring Boot application. Now, we will create an application. We are using STS (Spring Tool Suite) IDE for Creating of application that involves the following steps.
Step 1. Create a Maven Project
First create a maven project by selecting spring starter project wizard from file menu of IDE. A snapshot is given below.
Now, select project type as Maven, provide project name, packaging, and Java version etc.
Here, select spring boot version and project dependencies from the available options. We are selecting web dependency for our project right now.
After finishing, STS IDE has created a project for us. Our project structure looks like the following snapshot.
Observe that it has added maven dependencies for our project and created some default files as well.
A pom.xml file that contains all configuration and dependencies information looks like the below.
This project includes a auto created Java file. This Java file provides entry point for the application. It imports classes and uses annotation that we will discuss later.
Although this application is ready as a stand-alone application and you can start it, we will add a controller to it. So, it can handle HTTP requests.
Creating a Controller
First create a Java class file.
Provide package and controller name.
After finishing, create a method inside the controller which will be called when an HTTP request is made.
Now, we are running SpringBootExampleApplication.java as a Java application. A snapshot is given below.
During running the application it produce the following output on the console.
On the console, it shows the status of application as Started SpringBootExampleApplication in 12.71 seconds (JVM running for 14.6)
This message shows that the application is started. Now, we can send HTTP request from the any browser to call the controller method.
Running on the browser
By default application runs on port 8080. So, we are passing /hello request by following the port number. It produces the message returned from the requested method from the controller.
The SpringApplication Class
The SpringApplication, inside the main is a Spring Boot class. It is used to bootstrap our application. It calls static method run() which takes two arguments, one is class type and second is string array. It starts auto-configured tomcat web server when Spring application is started.