Open In App

Spring Boot - Getting Started

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring Boot is a part of the larger Spring Framework ecosystem which is known for its comprehensive programming and configuration model for the modern Java-based enterprise applications. Spring Boot has emerged as a go-to framework for creating REST APIs, microservices, and web applications with less configuration. It eliminates the need for boilerplate code and configuration by offering opinionated defaults and auto-configuration, enabling developers to focus on writing business logic rather than wrestling with setup details. Spring Boot’s ability to run applications on embedded servers like Tomcat or Jetty further enhances its appeal, allowing developers to create production-ready applications quickly and efficiently.

Features of Spring Boot

Spring Boot simplifies the creation of Spring applications by offering several key features:

  • Auto-Configuration: Automatically configures Spring components based on the classpath, properties, and beans present in the Spring Boot application.
  • Standalone Applications: Spring Boot applications can run independently without requiring an external application server, thanks to embedded servers like Tomcat or Jetty.
  • Production-Ready Features: Spring Boot offers features such as built-in health checks, application metrics, and support for externalized configuration, making it easier to create production-ready applications.
  • Opinionated Defaults: Provides pre-set configurations that allow you to start with a functional setup without much customization.
  • Starter POMs: Spring Boot provides starter POMs that bundle dependencies for specific purposes. For example, spring-boot-starter-web is ideal for quickly setting up web applications.

Prerequisites:

Before starting with Spring Boot, ensure that you have the following:

  • Basic Knowledge of Java: Understanding core Java concepts such as Object-Oriented Programming (OOP), data types, and collections.
  • Familiarity with the Spring Framework: Basic understanding of Dependency Injection (DI) and Inversion of Control (IoC) in Spring.
  • Development Environment Setup: Java Development Kit (JDK) installed (preferably JDK 17 or later), an Integrated Development Environment (IDE) like IntelliJ IDEA.
  • Maven: Familiarity with Maven for building and dependency management.

Spring Boot Project Creation Process

Step 1: Set Up the Spring Boot Project

  • Open the Spring Initializr website.
  • Choose the following options:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 3.3.2 (or the latest stable version)
    • Project Metadata: Group: com.example, Artifact: demo
    • Dependencies: Add Spring Web, Lombok, and Spring Boot Dev Tools.
  • Click on "Generate" to download the project.
Project Metadata

Project Structure

After creating the project, extract the folder and open it in IntelliJ IDEA. The file structure should look like this:

Project Folder Structure

Step 2: Create the HelloController Class

HelloController.java: This class defines a simple REST API endpoint that returns a greeting message.

Java
package com.example.demo;

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

// Marks this class as a REST controller
@RestController
public class HelloController {

    // Maps HTTP GET requests to /greeting to this method
    @GetMapping("/greeting")
    public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        // Returns a greeting message, using the provided name or "World" by default
        return String.format("Hello, %s!", name);
    }
}
  • The @RestController annotation is used to define a controller and to indicate that the return value of the methods should be bound to the web response body.
  • The @GetMapping("/greeting") annotation maps HTTP GET requests to the /greeting URL to the greeting method.
  • The @RequestParam annotation is used to retrieve query parameters from the URL. If no name parameter is provided, it defaults to "World."

Step 3: Main Class

DemoApplication.java: This is the main class that bootstraps the Spring Boot application.

Java
package com.example.demo;

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

// Marks this class as a Spring Boot application entry point
@SpringBootApplication
public class DemoApplication {

    // Main method to run the Spring Boot application
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • The @SpringBootApplication annotation is a convenience annotation that combines the following:
    • @Configuration: Tags the class as a source of bean definitions for the application context.
    • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
    • @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example/demo package, allowing it to find the HelloController.
  • The SpringApplication.run() method is used to launch the Spring Boot application.

Step 4: Configure pom.xml file

pom.xml: The Maven configuration file that manages dependencies for your project.

XML
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!-- Starter for building web applications, including RESTful APIs -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Developer tools for hot-swapping and easier development -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- Lombok for reducing boilerplate code like getters and setters -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Starter for testing Spring Boot applications -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Plugin to package the application as a JAR -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • The spring-boot-starter-web dependency brings in everything needed to build a REST API using Spring MVC.
  • The spring-boot-devtools dependency allows hot-swapping, enabling developers to see changes without restarting the application.
  • The lombok dependency helps reduce boilerplate code, though it's optional.
  • The spring-boot-starter-test dependency includes libraries like JUnit and Mockito for testing.

Step 5: Run the Application

Once the project is set up, you can run it using the run button in IntelliJ IDEA or by executing the following command in the terminal:

./mvnw spring-boot:run
Application Started

The application will start running on port 8080.

Step 6: Test the Application

We can test the application using Postman or any other HTTP client:

Default Greeting:

GET http://localhost:8080/greeting

Output:

Postman UI


Custom Greeting:

We can customize the greeting by the passing the name parameter.

GET http://localhost:8080/greeting?name=John

Output:

Postman UI

Benefits of Using Spring Boot

  • Rapid Development: With the auto-configuration and starter templates, developers can get started quickly.
  • Embedded Servers: No need to deploy the application to an external server. Spring Boot applications are self-contained.
  • Microservices-Friendly: It can simplifies the creation and deployment of the microservices with the minimal configuration.
  • Production-Ready: It will comes with the built-in features like health checks, metrics and logging. Making it easy to the move from development to production.
  • Strong Ecosystem: It can be integrates seamlessly with the other Spring projects and has the large community and documentation support.

Conclusion

Spring Boot simplifies the process of creating production-ready applications with REST APIs, microservices, and less configuration. By following the steps above, we have set up a basic Spring Boot project with a REST API endpoint. This foundation can be expanded to include more complex functionalities and services as our application grows.


Similar Reads