Layered architecture
Skill rrezartprebreza/spring-boot-skills/skills/spring-boot-3/layered-architecture
Use when generating or modifying any Spring Boot class — controllers, services, repositories, DTOs, mappers, or configuration. Enforces strict layer separation and prevents business logic from leaking across boundaries.From its SKILL.md
npx -y skills add rrezartprebreza/spring-boot-skills --skill layered-architectureAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
6.1 KB, ~1.2k tokens by cl100k_base, as published. Nobody here has run it
Layered Architecture
Layer Rules
@RestController ← HTTP only. No business logic. No JPA entities in responses.
↓ DTOs
@Service ← All business logic lives here. Orchestrates repositories.
↓ Domain objects / Entities
@Repository ← Data access only. No business logic. Returns entities or projections.
↓ JPA / JDBC
Database
Controller Layer
- Handles HTTP: parsing requests, validating input (
@Valid), returning responses - Calls ONE service method per endpoint — no orchestration in controllers
- Never returns
@Entityclasses directly — always map to response DTOs - Never injects
@Repository— always goes through a@Service - Exception handling via
@ControllerAdvice, never try/catch in controllers
// ✅ GOOD
@PostMapping("/orders")
public ResponseEntity<OrderResponse> createOrder(@Valid @RequestBody CreateOrderRequest request) {
Order order = orderService.createOrder(request);
return ResponseEntity.status(HttpStatus.CREATED).body(OrderResponse.from(order));
}
// ❌ BAD — business logic in controller
@PostMapping("/orders")
public ResponseEntity<Order> createOrder(@RequestBody CreateOrderRequest request) {
if (request.getItems().isEmpty()) throw new RuntimeException("No items");
Order order = orderRepository.save(new Order(request)); // direct repo access
return ResponseEntity.ok(order); // returning entity
}
Service Layer
- Contains all business logic, validation rules, and orchestration
@Transactionallives here, not in controllers or repositories- Constructor injection only — never
@Autowiredfield injection - One service per aggregate root (OrderService, not OrderAndPaymentService)
- Returns domain objects or DTOs — never
HttpServletRequest/HttpServletResponse
// ✅ GOOD
@Service
@RequiredArgsConstructor
public class OrderService {
private final OrderRepository orderRepository;
private final InventoryService inventoryService;
@Transactional
public Order createOrder(CreateOrderRequest request) {
inventoryService.reserve(request.getItems());
Order order = Order.from(request);
return orderRepository.save(order);
}
}
// ❌ BAD — field injection, HTTP concern in service
@Service
public class OrderService {
@Autowired private OrderRepository orderRepository;
public ResponseEntity<Order> createOrder(...) { ... } // HTTP type in service
}
Repository Layer
- Extends
JpaRepository<Entity, ID>orCrudRepository - Custom queries via
@Queryor query derivation — no raw SQL unless unavoidable - Returns entities or Spring Data Projections — never raw
Object[] - No business logic — pure data access
DTOs
- Separate Request / Response DTOs — never use the same class for both
- Validation annotations (
@NotNull,@Size, etc.) on Request DTOs only - Static factory method
ResponseDto.from(Entity entity)for mapping - Use records for immutable DTOs (Java 16+)
// ✅ GOOD
public record OrderResponse(UUID id, String status, List<LineItemResponse> items) {
public static OrderResponse from(Order order) {
return new OrderResponse(order.getId(), order.getStatus().name(),
order.getItems().stream().map(LineItemResponse::from).toList());
}
}
Mapper Pattern
- Keep mapping logic out of controllers and services — use dedicated mapper classes or static factory methods
- Mapper is a plain class or utility — not a Spring bean unless it needs injected dependencies
- Entity → Response DTO: static method on the response DTO (
OrderResponse.from(order)) - Request DTO → Entity: static factory on the entity (
Order.from(request)) or a mapper class - Collection mapping: use
.stream().map(OrderResponse::from).toList()— never manual loops
// ✅ GOOD — dedicated mapper for complex mappings
public class OrderMapper {
public static OrderResponse toResponse(Order order) {
return new OrderResponse(
order.getId(),
order.getStatus().name(),
order.getItems().stream().map(OrderMapper::toLineItem).toList(),
order.getCreatedAt()
);
}
public static Order toEntity(CreateOrderRequest request, User user) {
Order order = Order.create(request.customerEmail(), user);
request.items().forEach(item ->
order.addItem(item.productId(), item.quantity()));
return order;
}
private static LineItemResponse toLineItem(OrderItem item) {
return new LineItemResponse(item.getProductId(), item.getQuantity(), item.getPrice());
}
}
Configuration Layer
@Configurationclasses live in aconfig/package — never inservice/orcontroller/- Configuration never imports service or controller classes
- Use
@ConfigurationPropertiesfor type-safe config — never raw@Valuefor groups of related settings - Bean definitions for infrastructure concerns only (RestTemplate, ObjectMapper, SecurityFilterChain)
Cross-Cutting Concerns
- Logging: use
@Slf4j— neverSystem.out.println - Validation:
@Validon controller parameters, custom validators as@Component - Exception handling: single
@RestControllerAdviceclass, never try/catch in controllers - Auditing:
@CreatedDate/@LastModifiedDatewith@EnableJpaAuditing
Gotchas
- Agent tends to put
@Transactionalon controllers — move it to services - Agent uses
@Autowiredfield injection — always use constructor injection (@RequiredArgsConstructor) - Agent returns
List<Entity>from controllers — always map toList<ResponseDto> - Agent creates
OrderAndInventoryServicegod classes — split by aggregate - Agent puts mapping logic inside controllers — extract to mapper class or DTO factory method
- Agent creates
@Configurationclasses that depend on@Servicebeans — configuration should only wire infrastructure
What ships with it: 5 files
5.4 KB alongside SKILL.md
examples/
- bad-service.java1.5 KB
- good-service.java1.2 KB
templates/
- CreateOrderRequest.java795 B
- OrderMapper.java1.0 KB
- OrderResponse.java911 B
Gives 0 of the 12 instructions most architecture codebase skills give in ~1.2k tokens
Counted across 858 of the 1,304 authors here whose files we hold, read 2026-09-06
- Apply the deletion test to identify shallow modulesin 32 of 858, across 31 files
- Read domain glossary and ADRs before exploringin 22 of 858, across 19 files
- Use Tailwind and Mermaid via CDN for reportsin 21 of 858, across 18 files
- Document architecture decision recordsin 20 of 858, across 12 files
- Offer to record ADRs for rejected candidatesin 17 of 858, across 14 files
- Limit primary navigation to four to seven itemsin 17 of 858, across 7 files
- Write HTML report to the system temp directoryin 17 of 858, across 14 files
- Read product marketing context before asking questionsin 16 of 858, across 6 files
- Use Mermaid graph TD for visual sitemapsin 15 of 858, across 5 files
- Ensure every page has at least one internal linkin 15 of 858, across 5 files
- Use ASCII tree format for page hierarchy draftsin 15 of 858, across 5 files
- Enforce lowercase URLs with hyphensin 15 of 858, across 5 files
Said here and by no other author read
- use constructor injection for all spring beans
- handle exceptions using controller advice classes
- use records for immutable data transfer objects
- place configuration classes in a dedicated package
- use configuration properties for type safe settings
- use slf4j for all logging requirements
Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once. Length counted with cl100k_base; the agent that loads this file may tokenize it differently.