Spring Boot

Language: Java

Web

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.

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.

Installation

maven: Add Spring Boot starter dependencies in your pom.xml
gradle: Add Spring Boot starter dependencies in your build.gradle

Usage

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.

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.

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.

Error Handling

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.