Rest api conventions
Skill rrezartprebreza/spring-boot-skills/skills/spring-boot-3/rest-api-conventions
Use when generating REST controllers, response wrappers, DTOs, error handlers, or any HTTP-facing code. Defines response envelope, HTTP status mapping, pagination, and versioning.From its SKILL.md
npx -y skills add rrezartprebreza/spring-boot-skills --skill rest-api-conventionsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
4.7 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it
REST API Conventions
Response Envelope
All endpoints return a consistent envelope:
{
"success": true,
"data": { },
"error": null,
"timestamp": "2026-04-13T10:00:00Z"
}
Error response:
{
"success": false,
"data": null,
"error": {
"code": "ORDER_NOT_FOUND",
"message": "Order with id 123 not found",
"details": []
},
"timestamp": "2026-04-13T10:00:00Z"
}
ApiResponse Wrapper
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ApiResponse<T>(
boolean success,
T data,
ApiError error,
Instant timestamp
) {
public static <T> ApiResponse<T> ok(T data) {
return new ApiResponse<>(true, data, null, Instant.now());
}
public static <T> ApiResponse<T> error(String code, String message) {
return new ApiResponse<>(false, null, new ApiError(code, message, List.of()), Instant.now());
}
}
public record ApiError(String code, String message, List<String> details) {}
HTTP Status Mapping
| Scenario | Status |
|---|---|
| GET — found | 200 |
| POST — created resource | 201 |
| PUT/PATCH — updated | 200 |
| DELETE — deleted | 204 (no body) |
| Validation failure | 400 |
| Unauthenticated | 401 |
| Forbidden | 403 |
| Not found | 404 |
| Conflict (duplicate) | 409 |
| Unhandled server error | 500 |
URL Conventions
- Plural nouns for resources:
/orders,/users,/products - Kebab-case for multi-word:
/order-items, not/orderItems - Versioning in path:
/api/v1/orders - Nested resources max 2 levels:
/orders/{id}/items✅,/orders/{id}/items/{itemId}/notes❌ — flatten to/order-item-notes/{id} - IDs as UUIDs in path, never auto-increment integers exposed in URL
GET /api/v1/orders → list (paginated)
POST /api/v1/orders → create
GET /api/v1/orders/{id} → get one
PUT /api/v1/orders/{id} → full update
PATCH /api/v1/orders/{id} → partial update
DELETE /api/v1/orders/{id} → delete
GET /api/v1/orders/{id}/items → nested resource
Pagination
{
"success": true,
"data": {
"content": [...],
"page": 0,
"size": 20,
"totalElements": 150,
"totalPages": 8,
"last": false
}
}
Query params: ?page=0&size=20&sort=createdAt,desc
Use Spring Data Pageable in controllers:
@GetMapping
public ApiResponse<Page<OrderResponse>> list(Pageable pageable) {
return ApiResponse.ok(orderService.findAll(pageable).map(OrderResponse::from));
}
Cap the page size. A bare Pageable accepts ?size=100000 from any client — one request can
drag your whole table into memory. Spring's default cap is 2000, still too high for most APIs:
spring:
data:
web:
pageable:
default-page-size: 20
max-page-size: 100 # requests above this are silently clamped
Global Exception Handler
@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ApiResponse<Void>> handleNotFound(EntityNotFoundException ex) {
return ResponseEntity.status(404).body(ApiResponse.error("NOT_FOUND", ex.getMessage()));
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse<Void>> handleValidation(MethodArgumentNotValidException ex) {
List<String> details = ex.getBindingResult().getFieldErrors().stream()
.map(e -> e.getField() + ": " + e.getDefaultMessage()).toList();
return ResponseEntity.status(400)
.body(new ApiResponse<>(false, null, new ApiError("VALIDATION_FAILED", "Invalid input", details), Instant.now()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<Void>> handleGeneric(Exception ex) {
return ResponseEntity.status(500).body(ApiResponse.error("INTERNAL_ERROR", "An unexpected error occurred"));
}
}
Gotchas
- Agent returns raw objects without envelope — always wrap in
ApiResponse.ok(...) - Agent uses
ResponseEntity<Map<String, Object>>for errors — useApiResponse - Agent puts exception handlers in controllers — always use
@RestControllerAdvice - Agent uses
LongIDs in URLs — useUUID - Agent accepts unbounded
Pageable— setspring.data.web.pageable.max-page-sizeor one request can pull the whole table - Agent returns
Page<Entity>serialized directly — exposes Hibernate internals; map to DTOs first
What ships with it: 4 files
4.8 KB alongside SKILL.md
examples/
- bad-controller.java1.3 KB
- good-controller.java1.1 KB
templates/
- ApiResponse.java925 B
- GlobalExceptionHandler.java1.6 KB
Gives 1 of the 12 instructions most apis services skills give in ~1.1k tokens
Counted across 448 of the 471 authors here whose files we hold, read 2026-09-06
- Use HTTP status codes semanticallyin 25 of 448, across 11 files
- Return 201 with a Location header on createin 24 of 448, across 9 files
- Name resources plural, lowercase, kebab-casein 23 of 448, across 9 files
- Configure rate limiting with limit headersin 22 of 448, across 8 files
- Paginate list endpoints with cursor or offsetin 21 of 448, across 10 files
- Version APIs in the URL pathhere, and in 21 of 448, across 11 files
- Validate request input with a schemain 21 of 448, across 7 files
- Add pagination to all list endpointsin 18 of 448, across 15 files
- Match HTTP method to the operationin 12 of 448, across 6 files
- Return 400 or 422 with field-level detailsin 12 of 448, across 2 files
- Check ownership before returning resourcesin 12 of 448, across 2 files
- Limit query depth and complexityin 12 of 448, across 7 files
Said here and by no other author read
- Limit nested resources to two levels
- Expose UUIDs in URLs, not auto-increment IDs
- Accept Spring Data Pageable for list endpoints
- Handle exceptions via a global RestControllerAdvice
- Map entities to DTOs before serializing
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.