Spring Boot makes Java application development simple and efficient. With Spring Initializr, you can quickly set up a project, and using the free IntelliJ IDEA Free Community Edition, you can start coding immediately. In this guide, we’ll show you how to create a Spring Boot project, import it into IntelliJ IDEA, and build a simple REST API.
YouTube Video
We recommend watching the YouTube video below to learn how to create a Spring Boot project in IntelliJ IDEA (Free Community Edition) IDE using Spring Initializr and build Spring Boot REST API:
What You’ll Need
Java Development Kit (JDK 23):
Download and install JDK 23 from Oracle or OpenJDK.- Verify the installation:
java -version
- Verify the installation:
IntelliJ IDEA Community Edition:
Download the free version of IntelliJ IDEA from JetBrains.
Step 1: Create a Spring Boot Project with Spring Initializr
Visit Spring Initializr:
Go to Spring Initializr in your web browser.Fill in Project Details:
- Group:
com.example
- Artifact:
spring-boot-rest-api
- Name:
SpringBootRestApi
- Packaging: Jar
- Java Version: 23
- Group:
Add Dependencies:
- Click Add Dependencies and select:
- Spring Web: To build REST APIs.
- Spring Boot DevTools: For hot-reloading during development.
- Click Add Dependencies and select:
Download the Project:
- Click Generate to download the project as a ZIP file.
Extract the ZIP File:
Extract the downloaded project to a folder (e.g.,C:\Projects\SpringBootRestApi
).
Step 2: Open the Project in IntelliJ IDEA
Launch IntelliJ IDEA:
- Open IntelliJ IDEA Community Edition.
Open the Project:
- From the IntelliJ welcome screen, click Open.
- Navigate to the folder where you extracted the Spring Boot project and select it.
Wait for Dependencies:
- IntelliJ will automatically detect the
pom.xml
file (Maven project) and download the necessary dependencies.
- IntelliJ will automatically detect the
Step 3: Explore the Project Structure
Your project will look something like this:
spring-boot-rest-api/
├── src/
│ ├── main/
│ │ ├── java/com/example/springbootrestapi/
│ │ │ └── SpringBootRestApiApplication.java
│ │ └── resources/
│ │ ├── application.properties
├── pom.xml
- SpringBootRestApiApplication.java: The main class for your Spring Boot application.
- application.properties: Configuration file for the application.
- pom.xml: Maven’s configuration file to manage dependencies.
Step 4: Create a Simple REST API
Create a Controller:
- Inside the
com.example.springbootrestapi
package, right-click and select New > Java Class. - Name the class
HelloController
.
- Inside the
Add REST API Code:
- Open
HelloController.java
and paste the following code:package com.example.springbootrestapi; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/") public String sayHello() { return "Welcome to Spring Boot 3!"; } }
- Open
Step 5: Run the Application
Run the Application:
- Open the
SpringBootRestApiApplication.java
file. - Right-click and select Run 'SpringBootRestApiApplication'.
- Open the
Verify the API:
- Once the application starts, open your browser and go to:
http://localhost:8080/
- You should see the message:
Welcome to Spring Boot 3!
- Once the application starts, open your browser and go to:
Conclusion
You’ve successfully created a Spring Boot project, imported it into IntelliJ IDEA, and built a REST API. This basic setup can now be expanded to include more features, such as additional endpoints, services, or database connections. Happy coding!
Comments
Post a Comment
Leave Comment