Torna al blog
Sviluppo software

Spring Boot: how I build Java REST APIs that hold up in production

Spring Boot is the Java framework that made backend development accessible to anyone who knows Java. In this article I share how I build complete REST APIs: from zero to deployment, with dependency management, JPA, security and integrated tests.

Why Spring Boot changed the Java backend world

Spring Boot flipped the paradigm: instead of explicitly configuring everything, start with convention. A @SpringBootApplication annotation, a main() method, and Spring does the rest. It autoconfigures the embedded server (Tomcat), registers components found in the classpath, manages transitive dependencies. The code you write is business code, not infrastructure. This is the "convention over configuration" principle — and once internalized, you can never go back.

Minimal project: from zero to HTTP server in 5 minutes

With Spring Initializr you can generate a complete project in a few clicks. Spring Boot brings all transitive dependencies with compatible versions automatically. You don't manage compatibility between Jackson and Spring MVC or between Hibernate and Spring Data — the Spring Boot BOM (Bill of Materials) does that work.

Controller and REST: the layered architecture

The standard Spring Boot REST architecture has three layers: Controller (exposes HTTP endpoints, receives requests, delegates to Service), Service (contains business logic, orchestrates operations), Repository (manages data access via JPA/Hibernate). Key choices I always apply: constructor injection instead of @Autowired on fields (more testable), separate DTOs for input and output, ResponseEntity for cases where the HTTP code depends on the result.

Spring Data JPA: queries without SQL

Spring Data JPA is one of the components I appreciate most. You can write complex queries simply by declaring methods in the Repository interface — Spring Data interprets the method name and generates the corresponding JPQL query. For more complex cases I use the @Query annotation with JPQL or native SQL. In both cases, no manual JDBC — Spring Data manages the connection, result mapping, and transaction management.

Validation: validating input at the boundary

Validate everything coming from outside, trust internal code. Spring Boot integrates Bean Validation (Hibernate Validator) that lets you declare validation rules directly on DTO classes. With @Valid in the controller, Spring automatically validates the request body before the method executes. If validation fails, it responds with 400 Bad Request and error details — no manual validation code in the controller.

Centralized exception handling

Errors in a REST API must be consistent: same JSON format, correct HTTP codes, useful messages. The pattern I use is a global @RestControllerAdvice. With this handler, all application errors produce JSON responses with uniform structure. Clients can handle errors predictably without parsing text messages.

Tests with JUnit 5 and MockMvc

@WebMvcTest loads only the MVC layer (controllers, filters, advice) without loading the JPA context. Tests are fast and isolated. MockMvc simulates HTTP requests and lets you verify status codes, body, headers — all without network.

Why Spring Boot is still the right choice

Despite the proliferation of modern frameworks (Quarkus, Micronaut, Helidon), Spring Boot remains the most pragmatic choice for most Java backend projects. The ecosystem is enormous, documentation is excellent, community support resolves almost any problem in minutes of searching. The learning curve is higher than some lightweight frameworks, but the investment pays off: Spring Boot skills apply across thousands of existing projects throughout the industry.

Condividi