Backend6 min read

Java & Spring Boot

Build production-grade Java services with Spring Boot - dependency injection, Spring Data JPA, REST controllers, security, and testing.

JavaSpring BootJVMSpring Data JPAMicroservices

skill: Java & Spring Boot category: Backend tags: [Java, Spring Boot, JVM, Spring Data JPA, Microservices] readingTime: 6

Spring Boot's auto-configuration and convention-over-configuration make building production-grade Java services fast. The JVM ecosystem's depth (tooling, monitoring, ecosystem libraries) is unmatched for enterprise applications. Java 21's virtual threads (Project Loom) bring Go-like concurrency to the JVM.

Workflow

  1. Structure a Spring Boot applicationSpring Boot's layered architecture (Controller → Service → Repository) with dependency injection makes code testable and maintainable.
  2. Write REST controllers with validationSpring MVC controllers handle HTTP routing, parameter binding, and response serialization. Bean Validation (Jakarta Validation) enforces request constraints declaratively.
  3. Use Spring Data JPA efficientlyJPA Hibernate generates SQL from object graphs, but N+1 queries and Cartesian products are common. Understand fetch strategies to avoid them.
  4. Configure Spring Security with JWTSpring Security provides authentication and authorization. JWT tokens enable stateless authentication for APIs.
  5. Write comprehensive testsSpring Boot provides excellent testing support - MockMvc for controller tests, @DataJpaTest for repository tests, and @SpringBootTest for integration.

Output Format

Spring Boot: JPA entity + repository, @RestController with @Valid, @Transactional service layer, Spring Security JWT filter chain, and WebMvcTest + @Mock unit tests.

@Entity + JpaRepository<Entity, Long>
@RestController + @PostMapping + @Valid @RequestBody
@Service + @Transactional(readOnly=true) + @Transactional for writes
SecurityFilterChain: permitAll/authenticated/hasRole + JWT filter
@WebMvcTest + @MockBean + mockMvc.perform()

Examples

Input

Output

Edge Cases & Gotchas

  • N+1 queries with LAZY loading - JPA lazy loading fetches relations only when accessed. Accessing order.getItems() in a loop outside a transaction fires N+1 queries. Always eager-load or join-fetch what you'll use.
  • OpenEntityManagerInViewFilter - Spring Boot enables OEMIV by default - extends JPA session through the view layer, enabling lazy loading in templates. This causes N+1 in views and hides design problems. Disable it: spring.jpa.open-in-view=false.
  • Spring Boot DevTools in production - spring-boot-devtools (automatic restart on file changes) must never be in production. It's automatically disabled when running a packaged JAR (java -jar).
  • Virtual threads (Java 21+) - Spring Boot 3.2+ supports virtual threads: spring.threads.virtual.enabled=true. Virtual threads handle millions of concurrent connections without thread pool sizing - eliminates async/reactive complexity for I/O-bound apps.

When to Break These Rules

  • Use FastAPI or Node.js instead of Spring Boot for small teams without Java expertise - Spring Boot's learning curve (JPA, Spring Security configuration) is high for developers new to the ecosystem.
  • Use Quarkus or Micronaut instead of Spring Boot for serverless/container-first deployments where startup time matters - Spring Boot's startup (3-10s) is slow compared to native-compiled alternatives.

Reference Files

  • https://docs.spring.io/spring-boot/reference/
  • https://docs.spring.io/spring-security/reference/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.