Spring Boot - First Application

December 10, 20242 min read

Spring Boot - First Application

Spring Boot simplifies the process of building production-ready applications with the Spring Framework. In this guide, we’ll walk through creating your first Spring Boot application step by step.


🌟 Prerequisites

📋 Before starting, ensure you have the following:

  • Java Development Kit (JDK) 17+ installed
  • 📦 Maven or Gradle installed
  • 🖥️ A Java IDE (e.g., IntelliJ IDEA, Eclipse, or Visual Studio Code)

🛠️ Step 1: Create a Spring Boot Project

You can create your first Spring Boot project in two ways:

  1. Using Spring Initializr 🖱️
  • Visit Spring Initializr.
  • Configure the following:
    • 📂 Project: Maven
    • 👨‍💻 Language: Java or Kotlin
    • 🔄 Spring Boot Version: 3.0.0 (or the latest version).
    • 📜 Add dependencies: Spring Web
  • Click Generate to download the project files.
  1. Using IntelliJ IDEA 💻
  • Open IntelliJ IDEA.
  • Go to New Project > Spring Initializr.
  • Configure similar parameters as mentioned above.

📖 Step 2: Writing Your First Endpoint

Let’s write a simple endpoint to say hello:

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } }
package com.example.demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController @SpringBootApplication @RestController class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @GetMapping("/hello") fun sayHello(): String { return "Hello, Spring Boot!" }

▶️ Step 3: Run the Application

  1. Open a terminal in the project folder.
  2. Execute the command to run your application:
./mvnw spring-boot:run
  1. Access the endpoint at:
    http://localhost:8080/hello
    

Response:

Hello, Spring Boot!

This post covers the basics of creating a Spring Boot project, writing an endpoint, and running it successfully.