Skip to content

Spring Boot

The default way to build a Java service — auto-configuration over a vast, mature ecosystem.

Web & HTTPWebJava

What it is

Spring Boot is a framework for building production-ready Java applications quickly. It simplifies configuration, provides embedded servers, and integrates easily with Spring ecosystem projects like Spring Data, Spring Security, and Spring MVC.

Spring Boot allows developers to quickly create REST APIs, web applications, and microservices. It provides auto-configuration, embedded servers, and ready-to-use starters for common functionalities like data access, security, and messaging.

Best known for
Auto-configuration — sensible defaults derived from what is on the classpath
Licence
Apache 2.0
Watch for
Understand what a starter pulls in before adding it

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • Building production web services or APIs on the JVM in an organisation of any size
  • You want security, data access, messaging and observability to integrate without wiring
  • Hiring matters — the Spring skill pool is the largest in the Java world

Look elsewhere when

  • Startup time and memory are critical, such as short-lived serverless functions
  • You want to see exactly what your application does — auto-configuration hides a great deal

Installation

Add Spring Boot starter dependencies in your pom.xml

Getting started

The smallest useful thing you can do with it, and what each part means.

Creating a Spring Boot application
// MainApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
This is the entry point of a Spring Boot application. The `@SpringBootApplication` annotation enables auto-configuration and component scanning.
Simple REST Controller
// HelloController.java
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!";
    }
}
Defines a REST endpoint `/hello` that returns a simple greeting string.

Advanced usage

Where the library earns its place over a simpler alternative.

Using Spring Data JPA
// User.java
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class User {
    @Id
    private Long id;
    private String name;
    // getters and setters
}

// UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {}
Demonstrates creating an entity and repository using Spring Data JPA for database operations.
Exception Handling with @ControllerAdvice
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.http.ResponseEntity;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception e) {
        return ResponseEntity.status(500).body("Error: " + e.getMessage());
    }
}
Provides centralized exception handling for all controllers using `@ControllerAdvice`.
Using application.properties
# application.properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=pass
Configuration properties for server port and database connection.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

ApplicationContextException
Occurs when Spring context fails to start. Check bean definitions and dependency injections.
DataAccessException
Occurs during database operations. Ensure correct configuration and valid queries.
HttpMessageNotReadableException
Occurs when JSON request body cannot be parsed. Validate input and request payload.

Best practices

  • Use Spring Boot starters to avoid boilerplate dependency configurations.
  • Leverage `@Configuration` and `@Bean` for custom configurations.
  • Externalize configurations using `application.properties` or `application.yml`.
  • Write unit and integration tests using Spring Boot Test utilities.
  • Use profiles (`@Profile`) for environment-specific configurations.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

Spring Boot was created by Pivotal (now VMware) to simplify Spring application setup and development. Its goal was to reduce boilerplate code, provide sensible defaults, and enable rapid development of standalone, production-grade applications with minimal configuration.