springboot-patterns

Implements Spring Boot REST APIs with layered architecture, validation, caching, and error handling.

Updated Jun 28, 2026
One-click install
npx skills add https://github.com/412181-HerediaLara/ScaffoldingBE-FE --skill springboot-patterns-412181-heredialara
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: springboot-patterns
Source: https://github.com/412181-HerediaLara/ScaffoldingBE-FE/tree/main/BE/.agents/skills/springboot-patterns
Command: npx skills add https://github.com/412181-HerediaLara/ScaffoldingBE-FE --skill springboot-patterns-412181-heredialara

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building production-grade Spring Boot backends requires consistent patterns for controllers, services, repositories, validation, and error handling. This Skill provides proven code templates and architectural guidance so you avoid common pitfalls like fat controllers, missing validation, and insecure header handling. ## Core Features & Use Cases - Layered Architecture Templates: Ready-to-use patterns for Controller, Service, Repository, and DTO layers with constructor injection and transactions. - Cross-Cutting Concerns: Centralized exception handling with @ControllerAdvice, Bean Validation, caching with @Cacheable, async processing, and SLF4J logging. - Security-Aware Utilities: Rate limiting with Bucket4j including correct X-Forwarded-For handling behind reverse proxies, plus retry logic for external calls. - Use Case: When scaffolding a new REST endpoint for a game or market resource, apply these patterns to get pagination, DTO validation, and RFC 7807 error responses right the first time. ## Quick Start Ask the AI to create a Spring Boot REST endpoint with validation, pagination, and centralized error handling following these patterns.

Frequently Asked Questions about springboot-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a Spring Boot REST API with layers?

Use a Controller layer for endpoints and DTO validation, a Service layer with @Transactional for business logic, and a Spring Data JPA Repository for data access. Keep controllers thin and never expose entities directly; map them to response DTOs.

How to handle validation errors in Spring Boot globally?

Use @ControllerAdvice with an @ExceptionHandler for MethodArgumentNotValidException to return a 400 response with field-level error messages. Enable spring.mvc.problemdetails.enabled=true in Spring Boot 3 for RFC 7807 error responses.

How do I add caching to a Spring Boot service?

Enable caching with @EnableCaching on a configuration class, then annotate service methods with @Cacheable for reads and @CacheEvict for invalidation. Specify cache names and keys, for example @Cacheable(value = "market", key = "#id").

Can I trust X-Forwarded-For for rate limiting in Spring?

No, X-Forwarded-For is spoofable unless your app sits behind a trusted proxy. Configure server.forward-headers-strategy and register ForwardedHeaderFilter so request.getRemoteAddr() returns the real client IP, and ensure the proxy overwrites the header.

How do I implement rate limiting in a Spring Boot filter?

Extend OncePerRequestFilter and use Bucket4j to keep a token bucket per client IP in a ConcurrentHashMap. Consume one token per request and return HTTP 429 when the bucket is empty.

When should I use constructor injection vs field injection in Spring?

Prefer constructor injection for all production code because it makes dependencies explicit, supports final fields, and simplifies unit testing. Field injection with @Autowired hides dependencies and complicates testing.