Spring boot is a very popular framework used to create enterprise grade applications. Spring boot framework provides many starter dependencies, which provides required configurations for various dependencies like data, spring security, web configurations, etc. 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.

Creating Spring boot application with STS

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

  • 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 Next button.
  • Enter the project details like Name, Group, Artifact, etc as shown below. Here, Group field is our application package details. Artifact is the name of the project folder structure. Click on Next button once required details are filled.
  • Select the required starter dependencies from various available options and click on Finish button as shown below. In this window, we can select spring boot version also. In my case, I am using current latest available spring boot version(2.1.5). Select Web dependency as shown below. You can choose from other available dependencies based on requirement. Main advantage of using these starter dependencies is, spring boot automatically adds compatible versions of the selected dependencies and we do not have to worry about version incompatibility.
  • This will create a standard maven project structure with spring boot application starter class and a test class along with other resources as shown below. In case you are new to 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 our applications class path from maven repository. This may take a little time, if you are using maven for the first time. This is because the dependency jars are downloaded from remote repository and stored in local repository of our computer for the first time.
  • Upon selecting above option will open Update Maven Project window as shown below. Select Force Update of Snapshots/Releases option and click on OK button. This makes sure that the local repository is updated with latest releases of dependencies.
  • Once the Maven update is completed, we can see our project updated with required Maven dependency libraries as shown below.
  • 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>

Now Lets check our source file, generated by the spring boot starter. Navigate to /src/main/java/ under package com.asb.example and open the java file SpringBootExampleUsingStsApplication.java. Update the content with following. This class is the entry point of the spring boot application, to run the application, right click on the file and run as java application. Click here to learn more about how spring boot bootstrapping process works using starter class.

Here, we have added a Rest controller with a HTTP GET request handler mapped to “/”, which displays “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 standardHello World” output of our application. Well Done!!! 🙂

Using 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. You can also choose Maven/Gradle to build project. and click on Generate Project button to download the project. This will download the .zip file to your computer.

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.

Browse the unzipped file on your computer, and select the folder, click on Finish button. Your project is imported to the IDE now. Well Done 🙂

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

You may also interested in