Javatpoint Logo
Javatpoint Logo

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.

Spring Boot simple application 1

Now, select project type as Maven, provide project name, packaging, and Java version etc.

Spring Boot simple application 2

Here, select spring boot version and project dependencies from the available options. We are selecting web dependency for our project right now.

Spring Boot simple application 3

After finishing, STS IDE has created a project for us. Our project structure looks like the following snapshot.

Spring Boot simple application 4

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.

// pom.xml

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.

// SpringBootExampleApplication.java

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.

Spring Boot simple application 5

Provide package and controller name.

Spring Boot simple application 6

After finishing, create a method inside the controller which will be called when an HTTP request is made.

// HomeController

Now, we are running SpringBootExampleApplication.java as a Java application. A snapshot is given below.

Spring Boot simple application 7

During running the application it produce the following output on the console.

Spring Boot simple application 8

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.

Spring Boot simple application 9

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.






Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials