Creating First Spring Boot Application – Step By Step Guide

Spring Boot is a very popular framework for creating enterprise-grade applications. Spring boot framework also provides many starter dependencies, which provides required configurations for various dependencies like data, spring-security, web configurations, etc. Creating a spring boot application is a simple task.

In this article, we will learn about how to create our first spring boot application using STS(Spring Tool Suite) IDE and also other ways of creating the application.

Table of Contents

Creating Spring boot application with STS

Spring Tool Suite(STS) IDE is one of the popular IDE used by most spring developers. Also, the STS IDE provides built-in support to create spring boot applications. In this example, I am using STS version 4. Following are the steps to create a new spring boot application using STS.

Creating the project

Open STS, and select File > New > Project as shown in the below image.

This will open up a New Project window as shown below. Select Spring Starter Project option under Spring Boot Wizard and click on the Next button.

Enter the project details like Name, Group, Artifact, etc as shown below. Here, the Group field is our application package details. Artifact is the name of the project folder structure. Fill in the necessary details and click on the Next button.

Adding the dependencies

Select the required starter dependencies from various available options and click on the Finish button as shown below.

In this window, we can select the spring boot version also. Here, we are using the current latest available spring boot version(2.1.5). Also, select Web dependency as shown below.

We can also choose from other available dependencies based on requirements. The main advantage of using these starter dependencies is, spring boot automatically adds compatible versions of the selected dependencies. Also, we do not have to worry about version incompatibility.

This will create a standard maven project structure with a spring boot application starter class and a test class along with other resources as shown below.

In case you are new to the maven project, this is the standard maven project structure, which contains java source files under /src/main/java directory, test files under /src/test/java directory, and static, template, and property files/src/main/resources directory.

We also have a POM(Project Object Model) XML file, which contains all the project details which we added earlier and project dependencies.

To download added dependencies jars and to set up our project, Right-click on project > Maven > Update Project. This will download and add the dependency jars to the classpath of our application from the maven repository. The process may take a little time if you are using Maven for the first time.

This is because the maven downloads the dependency jars from the remote repository, and stores them in the local repository of our computer for the first time.

Upon selecting the above option will open the Update Maven Project window as shown below. Select the Force Update of Snapshots/Releases option and click on the OK button.

With that, the maven makes sure that the local repository is up to date with the latest releases of dependencies.

Once the Maven update is completed, we can see our project updated with the required Maven dependency libraries as shown below.

The pom.xml file

The POM file content created by the STS is shown 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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.asb.exampple</groupId>
	<artifactId>spring-boot-example-using-sts</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-example-using-sts</name>
	<description>example</description>
	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Creating a spring boot apllication controller class

Navigate to the /src/main/java/ folder under the package com.asb.example and open the java file SpringBootExampleUsingStsApplication.java.

Update the content with the following code. This class is the entry point of the spring boot application. To run the application, right-click on the file and run it as a java application.

Here, we have added a Rest controller with an HTTP GET request handler mapped to “/”. The URL displays the “Hello World!!” message on starting the application and upon accessing the URL localhost:8080/ on the browser.

package com.asb.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class SpringBootExampleUsingStsApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootExampleUsingStsApplication.class, args);
	}
	@RestController
	public class Rest{
	
		@GetMapping("/")
		public String helloWorld() {
			return "Hello World!!";
		}
	}
}

Following is the standard “Hello World” output of our application.

Using the Spring Initializr to create spring boot application

Go to https://start.spring.io/, fill out the project details like Group ID, Artifact details, dependencies, etc.

We can also choose Maven/Gradle to build the project. and click on Generate Project button to download the project in the zip format.

creating spring boot application

To import this project into your Eclipse/STS or any other IDE, just unzip the file and import it. In Eclipse/STS, select File >Import > Maven > Existing Maven Project as shown below.

creating spring boot application

Browse the unzipped file on your computer, and select the folder, click on the Finish button.

Finally, our project is imported to the IDE now.

creating spring boot application

Conclusion

In this article, we learned how to create a spring boot application using STS and also using Spring Initializr.