backend-code-guideline
Skill: backend-code-guideline
Purpose
Ensure all generated backend code is:
- module-based
- API-contract-safe
- layered correctly
- production-ready
- consistent with AGENTS.md and OpenAPI
Backend Architecture Standard
Every backend business module must live under:
backend/src/main/java/com/yourapp/modules/<module_name>/
Standard module structure:
controller/service/service/impl/mapper/entity/dto/vo/convertor/
Core Layering Rule
Backend flow must always be:
controller -> service -> service/impl -> mapper -> entity
Forbidden:
- business logic in controllers
- skipping service layer
- exposing entities directly to clients
- introducing repository/ unless explicitly required for JPA
Naming Rules
Classes
- Controller:
XController - Service:
XService - Service implementation:
XServiceImpl - Mapper:
XMapper - Entity:
X - Convertor:
XConvertor
Requests
CreateXRequestUpdateXRequest
Responses
XResponse
Controller Rules
Controllers must:
- use
@RestController - contain endpoint wiring only
- delegate business logic to service layer
- use request DTOs
- return response VOs wrapped in the standard API response envelope
Controllers must not:
- perform heavy business logic
- perform manual persistence logic
- return entity objects directly
Service Rules
Services must:
- define clear business operations
- enforce business rules
- coordinate persistence and conversion
- remain reusable and testable
Service implementations must:
- not leak persistence concerns to controllers
- not duplicate mapping logic unnecessarily
Mapper Rules
Use MyBatis Plus mappers as the default data access layer.
Mapper interfaces should:
- extend
BaseMapper<Entity>where appropriate - remain focused on DB access
- avoid business orchestration logic
Use XML mappers only if really needed, and place them under:
backend/src/main/resources/mapper/
Entity Rules
Entities must:
- map DB schema exactly
- use proper table mapping annotations
- follow schema-defined fields
- map snake_case DB names cleanly
Include base fields where required:
idcreatedAtupdatedAtdeletedAt
DTO Rules
Use DTOs for all request payloads.
Apply validation annotations where appropriate:
@NotNull@NotBlank@Size@Min@Max
DTOs must reflect OpenAPI request contracts.
VO Rules
Use VOs for all response payloads.
VOs must:
- expose only API-required fields
- avoid persistence-only/internal fields
- match OpenAPI response schemas
Convertor Rules
Put mapping logic into convertor/.
Typical mappings:
- DTO -> Entity
- Entity -> VO
- List<Entity> -> List<VO>
Avoid:
- duplicated mapping logic across services
- controller-level object mapping
- entity-to-response leakage
Standard API Response Contract
All successful backend responses must use:
{
"code": 0,
"message": "success",
"data": {}
}
Controllers must NOT return:
- raw entity
- raw VO
- raw list
- raw pagination object
Pagination Rule
Backend may use MyBatis Plus Page<Entity> internally.
But API responses for paginated endpoints must be:
{
"code": 0,
"message": "success",
"data": {
"content": [],
"page": 1,
"size": 10,
"total": 100
}
}
The pagination object must always be inside data.
Soft Delete Rule
Use deleted_at semantics.
Backend must:
- exclude soft-deleted rows from normal list/detail queries
- avoid hard delete unless explicitly required
- keep DB and API behavior aligned on soft delete
Error Handling Rule
Use global exception handling.
Goals:
- clean error responses
- no internal leak
- consistent HTTP semantics
- centralized handling
Security Rule
Where auth is involved:
- respect role boundaries
- do not expose sensitive fields
- keep admin behavior explicit
- align with OpenAPI security expectations
Forbidden Patterns
- business logic in controller
- exposing entity directly
- missing DTO layer
- missing VO layer
- duplicating mapping logic everywhere
- hardcoding API contract outside OpenAPI
- skipping wrapper response format
Final Goal
Generated backend code must be:
- clean
- layered
- contract-safe
- scalable
- production-ready
