Setting Up the Development Environment for Spring Boot
Introduction
In this quick guide, you go through the process of setting up your development environment for Spring Boot.
Required Tools
To get started with Spring Boot, you need to have the following tools installed on your system:
- Java Development Kit (JDK) — Install Latest Java JDK
- Integrated Development Environment (IDE) — Use IntelliJ IDEA or Spring Tool Suite
- Build Tool (Maven or Gradle) — Use Maven for a quick start
1. Java Development Kit (JDK)
Download JDK:
- Go to the official Oracle JDK download page or AdoptOpenJDK.
- Choose the latest version of JDK (Java 21 or later is recommended).
Install JDK:
- Follow the installation instructions specific to your operating system (Windows, macOS, or Linux).
- Set the
JAVA_HOME
environment variable to point to the JDK installation directory.
Verify Installation:
- Open a terminal or command prompt.
- Run the command:
java -version
- Ensure the output shows the correct version of the JDK installed.
2. Integrated Development Environment (IDE)
Popular IDEs:
IntelliJ IDEA:
- Download and install IntelliJ IDEA from the official JetBrains website.
- Choose the Community Edition for free access or the Ultimate Edition for advanced features.
Download and install Spring Tool Suite IDE:
3. Build Tools (Maven or Gradle)
Maven Installation:
Download Maven:
- Go to the official Apache Maven website.
- Download the latest version of Maven.
Install Maven:
- Extract the downloaded archive to a directory of your choice.
- Set the
M2_HOME
environment variable to point to the Maven installation directory. - Add the
bin
directory of Maven to thePATH
environment variable.
Verify Installation:
- Open a terminal or command prompt.
- Run the command:
mvn -version
- Ensure the output shows the correct version of Maven installed.
Gradle Installation:
Download Gradle:
- Go to the official Gradle website.
- Download the latest version of Gradle.
Install Gradle:
- Extract the downloaded archive to a directory of your choice.
- Set the
GRADLE_HOME
environment variable to point to the Gradle installation directory. - Add the
bin
directory of Gradle to thePATH
environment variable.
Verify Installation:
- Open a terminal or command prompt.
- Run the command:
gradle -version
- Ensure the output shows the correct version of Gradle installed.
Once you install JDK, Maven and IntelliJ IDEA ( or STS), you can start building Spring Boot projects.
Create a Spring Boot Project using Spring Initializr and Import in IntelliJ IDEA
What is Spring Initializr?
Spring Initializr is a web-based tool that simplifies the process of bootstrapping a new Spring Boot project. It provides a user-friendly interface to select project settings and dependencies.
Open Spring Initializr:
- Navigate to Spring Initializr in your web browser.
Configure Project Metadata:
- Group:
com.company
- Artifact:
first-springboot-app
- Name:
Demo
- Description:
First Spring Boot App
- Package name:
com.company.app
- Packaging:
Jar
- Java Version:
17
(or the latest version available)
Select Dependencies:
- Add the following dependencies:
- Spring Web
- Spring Boot DevTools (optional for hot reloading)
Generate and Download:
- Click the “Generate” button to download the project as a ZIP file.
Open IntelliJ IDEA:
- Launch IntelliJ IDEA from your installed applications.
Import the Project:
- On the welcome screen, click “Open or Import”.
- Navigate to the directory where you downloaded the Spring Initializr ZIP file.
- Select the ZIP file and click “Open”.
- IntelliJ IDEA will unzip the file and import the project.
Configure Maven (if needed):
- IntelliJ IDEA will automatically detect the Maven build file (
pom.xml
) and import the project. - If prompted, confirm any necessary Maven configurations.
Explore the Project Structure
Project Structure:
- Once the project is imported, you will see the following structure in the Project Explorer:
src/main/java
: Contains your application source code.src/main/resources
: Contains application properties and other resources.src/test/java
: Contains test cases for your application.pom.xml
: Maven configuration file.
Run Your Spring Boot Application
Open Application Class:
- Navigate to the main application class (annotated with
@SpringBootApplication
).
Run the Application:
- In IntelliJ IDEA, right-click the main application class and select “Run ‘DemoApplication’”.
- Alternatively, open a terminal, navigate to the project directory, and run
mvn spring-boot:run
.
Verify the Application:
- Open a web browser and go to
http://localhost:8080
. - You should see the default Spring Boot welcome page.
Implementing the “Hello World” REST API
Create a New Package:
- In the
src/main/java/com/company/api
directory, create a new package namedcontroller
.
Create the Controller Class:
- Inside the
controller
package, create a new class namedHelloWorldController
. - Add the following code to the
HelloWorldController
class:
package com.company.api.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String helloWorld() {
return "Hello, World!";
}
}
The HelloWorldController
class is a simple Spring Boot controller that defines a REST API endpoint.
@RestController
annotation is used to define a controller and indicates that the return value of the methods should be written directly to the HTTP response body. It's a combination of @Controller
and @ResponseBody
.
@GetMapping("/hello")
annotation maps HTTP GET requests to the /hello
endpoint. When a GET request is made to this URL, the helloWorld
method is invoked.
public String helloWorld()
: This method returns the string "Hello, World!" which is sent as the HTTP response body.
Running the Application
Open Application Class:
- Navigate to the main application class (annotated with
@SpringBootApplication
).
Run the Application:
- In IntelliJ IDEA, right-click the main application class and select “Run ‘DemoAPIApplication’”.
- Alternatively, open a terminal, navigate to the project directory, and run
mvn spring-boot:run
.
Verify the Application:
- Open a web browser and go to
http://localhost:8080/hello
. - You should see the message “Hello, World!”.
Conclusion
In this guide, you learned how to set up the development environment for Spring Boot, including installing the JDK, IDE, and build tools. You also learned how to create a simple Spring Boot project and implement a “Hello World” REST API.