🚧

Work in Progress

This blog is still under development. Content and features may change.

🚧

Getting Started with Spring Boot: A Comprehensive Guide

Learn the fundamentals of Spring Boot and how to build your first REST API with this powerful Java framework.

3 min read

Getting Started with Spring Boot: A Comprehensive Guide

Spring Boot has revolutionized Java development by simplifying the creation of production-ready applications. In this comprehensive guide, we'll explore the fundamentals of Spring Boot and build a complete REST API from scratch.

What is Spring Boot?

Spring Boot is an opinionated framework built on top of the Spring Framework that aims to simplify the bootstrapping and development of Spring applications. It provides:

  • Auto-configuration: Automatically configures your application based on the dependencies you have added
  • Standalone: Creates standalone Spring applications that can run with java -jar
  • Production-ready: Includes features like metrics, health checks, and externalized configuration

Setting Up Your First Spring Boot Application

Prerequisites

Before we begin, ensure you have:

  • Java 17 or later
  • Maven 3.6+ or Gradle 7+
  • An IDE (IntelliJ IDEA, Eclipse, or VS Code)

Project Structure

Let's create a simple REST API for managing books. Here's our project structure:

src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── bookstore/
│   │               ├── BookstoreApplication.java
│   │               ├── controller/
│   │               │   └── BookController.java
│   │               ├── model/
│   │               │   └── Book.java
│   │               └── service/
│   │                   └── BookService.java
│   └── resources/
│       └── application.yml

Creating Your First Controller

Let's start by creating a simple REST controller:

@RestController
@RequestMapping("/api/books")
@CrossOrigin(origins = "*")
public class BookController {
    
    private final BookService bookService;
    
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }
    
    @GetMapping
    public ResponseEntity<List<Book>> getAllBooks() {
        List<Book> books = bookService.findAll();
        return ResponseEntity.ok(books);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Book> getBookById(@PathVariable Long id) {
        return bookService.findById(id)
                .map(book -> ResponseEntity.ok(book))
                .orElse(ResponseEntity.notFound().build());
    }
    
    @PostMapping
    public ResponseEntity<Book> createBook(@Valid @RequestBody Book book) {
        Book savedBook = bookService.save(book);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedBook);
    }
}

Key Spring Boot Features

1. Auto-Configuration

Spring Boot automatically configures your application based on the dependencies in your classpath. For example, if you include spring-boot-starter-web, it automatically:

  • Configures a Tomcat server
  • Sets up Spring MVC
  • Configures JSON serialization

2. Dependency Injection

Spring Boot leverages Spring's powerful dependency injection. Use constructor injection for better testability:

@Service
public class BookService {
    
    private final BookRepository bookRepository;
    
    public BookService(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }
    
    public List<Book> findAll() {
        return bookRepository.findAll();
    }
}

3. Configuration Properties

External configuration is made easy with application.yml:

server:
  port: 8080

spring:
  datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    username: sa
    password:
  
  jpa:
    hibernate:
      ddl-auto: create-drop
    show-sql: true

Best Practices

1. Use Constructor Injection

Constructor injection is preferred over field injection as it makes testing easier and ensures immutability:

// Good ✅
@Service
public class BookService {
    private final BookRepository repository;
    
    public BookService(BookRepository repository) {
        this.repository = repository;
    }
}

// Avoid ❌
@Service
public class BookService {
    @Autowired
    private BookRepository repository;
}

2. Validation

Use Bean Validation for input validation:

public class Book {
    @NotNull
    @Size(min = 1, max = 255)
    private String title;
    
    @NotNull
    @Size(min = 1, max = 100)
    private String author;
    
    @Min(0)
    private BigDecimal price;
}

3. Error Handling

Implement global exception handling:

@ControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(EntityNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleEntityNotFound(EntityNotFoundException ex) {
        ErrorResponse error = new ErrorResponse("ENTITY_NOT_FOUND", ex.getMessage());
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
    }
}

Testing Your Spring Boot Application

Spring Boot provides excellent testing support. Here's an example integration test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class BookControllerIntegrationTest {
    
    @Autowired
    private TestRestTemplate restTemplate;
    
    @Autowired
    private BookRepository bookRepository;
    
    @Test
    void shouldCreateBook() {
        Book book = new Book("Effective Java", "Joshua Bloch", new BigDecimal("45.99"));
        
        ResponseEntity<Book> response = restTemplate.postForEntity("/api/books", book, Book.class);
        
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
        assertThat(response.getBody().getTitle()).isEqualTo("Effective Java");
    }
}

Conclusion

Spring Boot dramatically simplifies Java web development by providing sensible defaults and auto-configuration. Key takeaways:

  1. Start with Spring Initializr to bootstrap your project quickly
  2. Embrace convention over configuration but customize when needed
  3. Use constructor injection for better testability
  4. Implement proper error handling and validation
  5. Write comprehensive tests using Spring Boot's testing features

With these fundamentals, you're well on your way to building robust, production-ready Spring Boot applications. In the next post, we'll dive deeper into Spring Data JPA and database integration.

Further Reading