Spring Boot Project in STS ( Spring Tool Suite)
The STS (Spring Tool Suite) IDE is an eclipse-based IDE for developing the Spring Boot applications. It is the preferred IDE for importing and developing the Spring Boot Project. We can create or import Spring Boot projects in STS IDE. Let’s see some Spring Boot examples using STS IDE.
In this section, we will see:
How to Create a Spring Boot Project Using STS (Spring Tool Suite)
How to Import a Spring Boot Project Using the STS (Spring Tool Suite)
Let’s see how to create a Spring Boot project in STS IDE:
How to Create a Spring Boot Project Using STS ( Spring Tool Suite)
Below are the steps to create a Spring Boot Project using the STS:
- Open Spring Tool Suite
- Navigate to File menu-> New-> Maven Project
- Select the maven-archetype-quickstart and select Next from this window.
- Provide the Project Details
- Configure App.java file
- Configure pom.xml file
- Add Java Version inside the properties of pom.xml
- Add Dependencies in pom.xml file.
- Create a class file
- Define the run() method
- Add annotations in the class
- Run the Application
However, the above steps seem like a complicated process. But, these steps are quite simple only need some attention. The Spring Initializr provides all of the above configurations by default.
Let’s understand each step in detail:
Step1: Open Spring Tool Suite
The First step is to open the STS tool.
Step2: Navigate to File Menu-> New-> Maven Project
To create a new Spring Boot project, navigate to File Menu-> New-> other menu.
Now it will open a select wizard, search for the Maven Project, and click Next to continue:
Step3: Select the maven-archetype-quickstart
Now, it will open a new maven project setup. Here, we can find a variety of maven projects. For the startup, select the maven-archtype-quickstart and hit Next to continue:
Step4: Provide the Project Details
in this step, enter the project details such as Group Id and Artifact Id. We have provided Group Id com.javasterling and Artifact Id spring-boot-sts-demo. Now, click on the Finish to complete the process.
Now, we have created a maven project in Spring Tool Suite. The project structure will look as follows:
Let’s configure it for Spring Boot.
Step5: Configure the App.java file
The App.java file can be found under the src/main/java directory within the package com.javasterling.Spring_Boot_Sts_Demo package. We don’t need to add any manual configuration to the App.java file; the following code is automatically generated:
App.java:
package com.javasterling.Spring_Boot_Sts_Demo;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Step6: Configure pom.xml file
A pom.xml file will be automatically generated to the Maven project. The default configuration code can be found in it:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.javasterling</groupId>
<artifactId>Spring-Boot-Sts-Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Boot-Sts-Demo</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Step7: Add Java Version in pom.xml file
Add a Java Version within the properties tag of the pom.xml file. For example, we are using Java 8, add the following statement in the pom.xml file:
<java.version>1.8</java.version>
Step8: Add Dependencies in pom.xml file
To make this project a Spring Boot project, we have to add Spring Boot dependencies. Add spring boot starter parent and spring boot starter web dependencies in the pom.xml file. The parent is used to declaring that our project is a child to this parent project.
Add the following dependency within the dependencies tag in pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
</dependency>
spring boot starter web:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
Now, save this file by stroking CTRL+S keys. When we save this file, it will start downloading the required jar files for Spring Boot. So, make sure you are connected to the internet.
Step9: Create a class file
Now, create a class file with the name SpringBootStsDemo in the package com.javasterling. To create a file, right-click on the package, navigate to the New -> Class -> menu, and provide the class details. Click on the Finish to create.
It will create a class file with the specified details.
Step10: Define the run() method
Once the class file is created, call the static method run() of SpringApplication class. Below is the code to call the static method run() and passing the class name as an argument.
SpringApplication.run(SpringBootStsDemo.class, args);
Step11:Add annotations in the class
Now, add an annotation in this class by adding an annotation @SpringBootApplication.
A single @SpringBootApplication annotation enables the following annotations in our application:
@EnableAutoConfiguration
: It enables the Spring Boot auto-configuration mechanism.@ComponentScan
: It scans the package where the application is located.@Configuration
: It allows us to register extra beans in the context or import additional configuration classes.
Now, our class file will look as follows:
SpringBootStsDemo.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootStsDemo {
public static void main(String[] args) {
SpringApplication.run(SpringBootStsDemo.class, args);
}
}
We are all done and created our first application in STS IDE. Now, run our application.
Step12: Run Application
Now, run our application either by right-clicking on the project or the SpringBootStsDemo class file and select the Run As-> Java Application. It will produce the following output:
As we can see from the output, the console is displaying that the application is up in 5.489 seconds and running.
Hence, the above guide is for developing the Spring Boot application in STS IDE. Now, let’s understand how to import a project in STS ( Spring Tool Suite).
How to Import a Spring Boot Project Using STS ( Spring Tool Suite)
To import a Spring Boot project in STS (Spring Tool Suite) is a straight forward process. Follow the below steps to import and run a Spring Boot Project:
- Open Spring Tool Suite
- Navigate to File -> Open Projects From File System
- Browse Project From the File System
- Select Project and Click Open
- Run Application
Step1: Open Spring Tool Suite
To import project in Spring Tool Suite, the first step is to open it.
Step2: Navigate to File -> Open Projects From File System
Now, Navigate to the File menu-> Open projects from the file system. Select the Directory option to browse the local directory.
Step3: Brose Project From File System
Now navigate the directory where you have downloaded the project and select it.
Step4: Select project and Click Open
It will select our project. Now, click Finish to complete the process.
Hence, we have successfully imported a Spring Boot Project in STS. Now, it will take a while to load the dependencies and download the required files. Makes sure you are connected to an internet connection.
Now, we are ready to run our application.
Step5: Run Application
To run this project, right-click on the project and select Run As-> Java Application. It may ask you to select the Java Application. Select the SpringBootDemoApplication and hit OK to continue.
It will take a few seconds to load the application content and run our project.
From the above snap of output, we can see the application is up in 1.642 seconds and running.
Download Projects: