Hi there, Today I'll walk you through how to create your first spring boot web application using the spring initializr tool which is powered by the Spring Initializr and the Pivotal Web Service.
This tutorial is for the absolute beginner and I'm gonna demonstrate you two different ways of creating a spring boot application.
- Using the Spring Initializr
- Using the Spring Tool Suite IDE
Prerequisite
- We need an IDE for creating writing the codes, here I'll use the Spring tool suite, you can use your favorite an IDE
- Java 1.8+
- Maven
- and a web browser to visit Spring Initializr
What is Spring boot
The Spring boot is a Open source framework for the Java developers. The Spring boot framework is used to build the stand-alone spring application.
The spring boot is also used to build the micor-services. The micro-service is the most popular architecture in todays era.
Advantages of the Spring boot
The Spring boot offer the various advantages and some of them are as follows
The Spring boot is easy to learn and develop the web or as well as the mico-service applications
Spring boot increase the productivity. As in the spring boot application, Java developer is not necessary to spend much time on the configuration and setup
Spring boot framework reduce the developement time. As we discuss in earlier point, The Java developer is not required to spend time on setup So, it reduce the developemnt time as well.
Spring boot provide the annotation configuration
Spring boot offer the application.properties to override the default setting of the spring boot and to configure as well
How to create the Spring boot application Using Spring initializr
Before getting started with spring initializr let's understand what is spring initialize. The spring initializr is a web tool, which is powered by the Spring Initializr and the Pivotal Web Service, to create Spring boot application and the URL is Spring initializr.
Let's create the Spring boot web application using the Spring initializr.
Launch your favorite web browser and search in Spring Initializr in your search engine.Once you visit to the Spring initialize tool, The sprint initializr looks like this.
Inside Project Metadata, fill Group as com.LearnJava (You can provide your web address name here as well)and in the Artifact fill FirstSpringBoot (well you can provide your project name here, but in this tutorial, I'll use FirstspringBoot).
For this tutorial, I'll use java 8, you can use your desire Java version as well, So make sure the Java version is selected which you have installed in your machine.
If you want to install Java 11 in your machine, I have demonstrate How to install Amazon Corretto JDK 11 on Windows 10 and explain why you should use Amazon Corretto 11.
Once you fill the project details information, Now you can add dependency from the Dependencies section. I'm creating a Rest API so I'll choose the following dependency. Right-click on the ADD DEPENDENCIES or press CTRL + B as shown in the image suggested.
After you click on ADD DEPENDENCIES or press CTRL + B, you will get the following screen.
Now search the Spring Web to create a Rest API.
Following adding the spring web dependencies, it will list in Dependencies section as showing in the image.
Now click on the generate to download the spring boot project. once you click on generate, The Save as screen will pop up. Make a note on where you saved this file, we will import this project in IDE.
After downloading the file from the spring initializer, unzip it on your favorite location, It is the good practice to create or separate your programming section to your personal directories.
How to Import Downloaded spring boot project from spring initializer in Eclipse or Spring tool suit
After Unzip the project, Now you can import your FirstBootProject in your Spring tool suit or eclipse or else in your preferred IDE. I'm using spring tool suite, Launch your Eclipse or Spring tool suit and now right-click on File and find import.
After clicking on the import button you will get the following screen.
Make sure you expand the Maven folder and select Existing Maven Project as shown in the above image. Now tap the Next button and you will get the following screen.
In the Root Directory, browse your unzip downloaded FirstSpringBoot project which you have downloaded from Spring Initializer. Make sure you have selected the path till the FirstSpringBoot folder path and check the pom.xml just like the below image and tap the Finish button.
After snapping the finish button, your IDE will download all the essential dependencies, which spring boot needs to function and it will take some time to download all the essential dependencies and if you are wondering, how long it will take, well it's depend on your internet speed.
Yes, you read correctly you need a working internet connection to setup the Spring boot application project.
After all import complete, check-in the project explorer and expand teh FirstSpringBoot project and you will get the following project structure.
Now if you open FirstSpringBootApplication, you will get the following code. @SpringBootApplication is the annotiona, which means it is the entry point of the spring boot application.
package com.LearnJava.FirstSpringBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstSpringBootApplication.class, args);
}
}
Click on the pom.xml from project explorer and you will find the configuration of the depenedncies and the project here
<?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 https://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.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.LearnJava</groupId>
<artifactId>FirstSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FirstSpringBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
congratulation, you have created your first spring boot application using the spring initializr.
Conclusion
In this tutorial, we have learned what is spring boot framework and the what are the advantages of the spring boot framework. We have discuss step by step how to create an first spring boot application using the spring initializr tool.
(getButton) #text=(Next: Create Rest API using Spring Boot) #icon=(link) #color=(#2339bd)