Create Spring Boot application using initializr in 5 minutes
What is Spring Initializr
Spring Initializr is a web-based tool that simplifies creating and setting up Spring Boot projects. It is straightforward for the developers to select the necessary configuration for their projects. The Spring Initializr tool takes care of the following configuration for any Spring-based project.
- Build tool (Maven or Gradle) to build the application.
- Spring Boot version(Dependencies are added based on the version).
- Dependencies required for the project.
- Language and its version.
- Project Metadata like name, packaging (Jar or War), package name, etc.
With all the provided information, the Spring Initializr generates the Spring project structure. We can use the Spring Initializr either from the Web or IDE or Command-Line.
The Spring Initializr UI has the following options:
- Project: Using this one can create a Maven or Gradle project i.e. Maven or Gradle can be used as a build tool. The default option is Maven Project. The Maven project is used in the entire tutorial.
- Language: Spring Initializr provides Java, Kotlin, and Groovy as a programming language for the project. Java is the default option.
- Spring Boot Version: Using this one can select the Spring Boot version for their project. Spring Boot latest Version is 3.2.0(M3). The SNAPSHOT versions are under development and are not stable.
- Project Dependencies: Dependencies are artifacts that we can add to the project. I am selecting Web Dependency.
- Project Metadata: It contains the information about the project. Information in the metadata does include below key points:
— Group ID: It is the ID of the project group
— Artifact: It is the name of the Application
— Name: Application name
— Description: It contains information about the project
— Package name: It is the combination of Group and Artifact Id
— Packaging: Using this Jar or War packaging can be selected
In this guide, we’ll walk you through the steps to create and run a basic Spring Boot application in your local environment using Spring Initializr.
Prerequisites
Before we start, make sure you have the following prerequisites in place:
- Java Development Kit (JDK): Spring Boot is Java-based, so you need to have a JDK (preferably Java 8 or higher) installed on your system. I’m using Java 17 and higher versions.
- Integrated Development Environment (IDE): You can use any Java IDE you prefer, but for this tutorial, I’ll use IntelliJ IDEA.
Step 1: Open Spring Initializr
- Open your web browser and navigate to **https://start.spring.io/**, which is the Spring Initializr web application.
- You’ll be presented with various options to configure your project. Here’s what you need to select:
Project: Choose “Maven Project” or “Gradle Project,” depending on your preference. For this tutorial, I’ll use Gradle. Find Gradle Vs Maven to know better
Language: Select “Java.”
Spring Boot: Choose the desired Spring Boot version.
Project metadata: Fill in the details as follows:
— Group: use your organization’s domain name in reverse order, e.g., “com.learning.”
— Artifact: give your project an artifact name, e.g., “demo.”
— Name: enter a name for your project, e.g., “SpringBootDemo.”
— Description: this field is optional.
— Package name: define your base package, e.g., “com.learning.demo.”
— Packaging: choose “Jar” for a standalone JAR file. - Click the “Generate” button, and Spring Initializr will create a ZIP file containing your project.
Step 2: Import the project into your IDE
- Download the ZIP file generated by Spring Initializr.
- Open your IDE (IntelliJ IDEA in this case).
- Select
File -> New -> Project from Existing Sources
and choose the downloaded ZIP file. Make sure to select "Import project from external model" and choose "Gradle." - Follow the prompts to import the project into your IDE.
Step 3: Build and run your Spring Boot application
- Once the project is imported, you’ll see it in your project explorer.
- Navigate to the
src/main/java
directory and open theDemoApplication.java
file.
- You’ll find a
main
method in this file. Right-click on it and selectRun Demo Application
. - Your Spring Boot application will start, and you can see the progress in the console. Once it’s up and running, you’ll see a message indicating that your application has started.
- Open a web browser and navigate to
http://localhost:8080
. You should see a "Whitelabel Error Page" because we haven't defined any specific endpoints yet.
Step 4: Create a simple REST endpoint
- Create a new Java class, e.g.,
HelloController.java
, in the same package as your main application class. - Add the following code to create a basic REST endpoint:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping
public String sayHello() {
return "Hola! Welcom to spring Boot";
}
}
- Save the file, and your application will automatically reload with the new endpoint available at
http://localhost:8080/hello
. - Open your web browser and navigate to
http://localhost:8080/hello
. You should see the message "Hola! Welcom to spring Boot
"
You’ve successfully created and run a Spring Boot application using Spring Initializr in your local environment. You can now explore Spring Boot’s features and start building more complex applications.
Happy coding!