Day 1: Introduction to Spring Boot

Welcome to the Spring Boot Blueprint: Craft Confident Code series! Over this journey, we’ll transform you from a curious beginner to a confident Java developer, building modern, production-ready applications with Spring Boot. Today, we kick things off with an introduction to Spring Boot and a hands-on "Hello World" REST API. Let’s dive in!

What is Spring Boot?

Spring Boot is a powerful framework built on top of the Spring Framework, designed to simplify Java application development. If you’ve ever wrestled with complex configurations in traditional Spring or struggled with setting up servers, Spring Boot is here to save the day. It offers:

  • Auto-configuration: Spring Boot automatically configures your application based on dependencies, reducing boilerplate code.

  • Embedded servers: No need to install external servers like Tomcat; Spring Boot bundles them for you.

  • Rapid development: With sensible defaults and tools like Spring Initializr, you can start coding in minutes.

  • Production-ready features: Built-in support for monitoring, security, and deployment.

Whether you’re building REST APIs, web apps, or microservices, Spring Boot streamlines the process, letting you focus on writing code rather than managing configurations.

Why Use Spring Boot?

Spring Boot is a favorite among Java developers for good reasons:

  • Speed: Get projects up and running quickly with minimal setup.

  • Flexibility: Works for small apps or large-scale enterprise systems.

  • Community: A massive ecosystem with libraries, tutorials, and active support.

  • Scalability: Easily integrates with databases, cloud platforms, and more.

In this series, we’ll explore these benefits hands-on, starting with a simple REST endpoint today.

Setting Up Your First Spring Boot Project

Let’s create a basic Spring Boot application using Spring Initializr, a web-based tool that generates a project skeleton for you.

Step 1: Generate the Project

  1. Visit start.spring.io.

  2. Set the following options:

    • Project: Maven (or Gradle, if you prefer).

    • Language: Java.

    • Spring Boot Version: Use the latest stable version (e.g., 3.2.x as of May 2025).

    • Group: com.example.

    • Artifact: hello-springboot.

    • Dependencies: Add Spring Web (for REST APIs).

  3. Click Generate to download a ZIP file.

  4. Unzip the file and open it in your IDE (e.g., IntelliJ IDEA, Eclipse, or VS Code).

Your project structure will look like this:

hello-springboot/
├── src/
│   ├── main/
│   │   ├── java/com/example/hellospringboot/
│   │   │   └── HelloSpringbootApplication.java
│   │   ├── resources/
│   │   │   └── application.properties
├── pom.xml

Step 2: Understand the Main Application

Open HelloSpringbootApplication.java. It should look like this:

package com.example.hellospringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloSpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloSpringbootApplication.class, args);
    }
}

The @SpringBootApplication annotation is the heart of your app, enabling auto-configuration, component scanning, and more. The main method starts the embedded server (Tomcat by default).

Step 3: Create a "Hello World" REST Endpoint

Let’s add a simple REST endpoint. Create a new file HelloController.java in the com.example.hellospringboot package:

package com.example.hellospringboot;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Spring Boot!";
    }
}
  • @RestController: Marks this class as a REST controller, handling HTTP requests.

  • @GetMapping("/hello"): Maps HTTP GET requests to /hello to the sayHello method.

Step 4: Run the Application

  1. In your IDE, run the HelloSpringbootApplication class (or use the command line: mvn spring-boot:run).

  2. Open a browser or Postman and visit http://localhost:8080/hello.

  3. You should see: Hello, Spring Boot!

Congratulations! You’ve just built and run your first Spring Boot REST API.

What’s Happening Behind the Scenes?

When you run the application:

  1. Spring Boot’s auto-configuration detects the Spring Web dependency and sets up an embedded Tomcat server.

  2. The @RestController is scanned and registered to handle requests.

  3. Visiting /hello triggers the sayHello method, returning a plain text response.

Try It Yourself

To solidify your learning, try these quick exercises:

  1. Modify the /hello endpoint to return a different message, like Welcome to Spring Boot!.

  2. Add another endpoint, e.g., /greet, that returns Greetings from Spring!.

  3. Explore application.properties in the resources folder and try changing the server port by adding server.port=8081.

What’s Next?

Tomorrow, we’ll dive into the Spring Boot project structure, exploring key files and annotations to deepen your understanding. By the end of this series, you’ll build, secure, and deploy a full-fledged application!

Resources:

  • Sample Code on GitHub

  • Spring Initializr

  • Spring Boot Documentation

Happy coding, and see you in Day 2!

0
Subscribe to my newsletter

Read articles from Prathamesh Karatkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prathamesh Karatkar
Prathamesh Karatkar