REST API Design Patterns
HTTP Methods & Status Codes
| Method |
Purpose |
Idempotent |
Success Code |
GET |
Read |
Yes |
200 |
POST |
Create |
No |
201 + Location |
PUT |
Full replace |
Yes |
200 |
PATCH |
Partial update |
No |
200 |
DELETE |
Remove |
Yes |
204 |
Key codes: 201 (Created + Location), 202 (Async), 204 (No content), 400 (Validation), 401 (Unauthenticated), 403 (Forbidden), 404 (Not found), 409 (Conflict), 422 (Semantic), 429 (Rate limited).
URL Conventions
- Resource mapping (controller):
/api/${resource}
- Path APIs (method):
/${versioning}/...
- Full URL:
/api/${resource}/${versioning}/...
# Controller: @RequestMapping("/api/users")
GET /api/users/v1 # List
POST /api/users/v1 # Create
GET /api/users/v1/123 # Get by ID
PUT /api/users/v1/123 # Replace
DELETE /api/users/v1/123 # Delete
GET /api/users/v1/123/orders # Nested (max 2 levels)
# Controller: @RequestMapping("/api/orders")
POST /api/orders/v1/123/cancel # Action as sub-resource
Rules: plural nouns, kebab-case, lowercase, no trailing slash, no verbs. Version on method, NOT resource mapping — enables per-resource bumps.
Error Format — RFC 7807
{
"type": "https://api.example.com/problems/validation-error",
"title": "Validation Error",
"status": 400,
"detail": "Request validation failed",
"errors": [{"field": "email", "message": "must be a valid email"}]
}
Enable: spring.mvc.problemdetail.enabled: true. Use @RestControllerAdvice with ProblemDetail.
Pagination
| Type |
Best For |
Notes |
Offset (page=0&size=20) |
Admin UIs |
Simple; slow at large offsets |
| Cursor (opaque token) |
Feeds, infinite scroll |
Consistent; no drift |
Keyset (afterId=X) |
Large datasets |
Fastest; needs composite index |
Cap size at 100. Prefer cursor/keyset.
Versioning
Version in method path: /api/{resource}/{version}/.... v1 is forever — backward compatible. Optional fields for minor changes. Breaking changes = new version. Use Sunset + Deprecation headers.
Validation & Rate Limiting
@Valid on all @RequestBody. Bean Validation: @NotBlank, @Email, @Size, @NotNull.
- Whitelist sort fields to prevent injection.
- Rate limit headers:
X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After.
Checklist
References
- references/design-patterns.md — Cursor/keyset pagination code, filtering/sorting, field selection, OpenAPI config
- references/operations-docs.md — File upload/download, idempotency, bulk operations, long-running operations
Related Skills
- summer-rest — Summer Framework handler pattern, ResponseFactory, exception handling
- spring-webflux-patterns — Controller implementation patterns (MVC + WebFlux)
- spring-security — Rate limiting headers, CORS, authentication for APIs
- architecture — Hexagonal interface layer design for REST endpoints
1---2name: api-design3description: REST API design patterns for Spring Boot — HTTP methods, status codes, URL conventions, RFC 7807 ProblemDetail errors, pagination, versioning, validation, and OpenAPI documentation. Use when designing REST endpoints, choosing HTTP status codes, implementing error responses, adding pagination to list APIs, versioning APIs, or generating OpenAPI/Swagger specs.4---56# REST API Design Patterns78## HTTP Methods & Status Codes910| Method | Purpose | Idempotent | Success Code |11|--------|---------|------------|--------------|12| `GET` | Read | Yes | 200 |13| `POST` | Create | No | 201 + Location |14| `PUT` | Full replace | Yes | 200 |15| `PATCH` | Partial update | No | 200 |16| `DELETE` | Remove | Yes | 204 |1718Key codes: 201 (Created + Location), 202 (Async), 204 (No content), 400 (Validation), 401 (Unauthenticated), 403 (Forbidden), 404 (Not found), 409 (Conflict), 422 (Semantic), 429 (Rate limited).1920## URL Conventions2122- **Resource mapping** (controller): `/api/${resource}`23- **Path APIs** (method): `/${versioning}/...`24- **Full URL**: `/api/${resource}/${versioning}/...`2526```27# Controller: @RequestMapping("/api/users")28GET /api/users/v1 # List29POST /api/users/v1 # Create30GET /api/users/v1/123 # Get by ID31PUT /api/users/v1/123 # Replace32DELETE /api/users/v1/123 # Delete33GET /api/users/v1/123/orders # Nested (max 2 levels)3435# Controller: @RequestMapping("/api/orders")36POST /api/orders/v1/123/cancel # Action as sub-resource37```3839Rules: plural nouns, kebab-case, lowercase, no trailing slash, no verbs. Version on method, NOT resource mapping — enables per-resource bumps.4041## Error Format — RFC 78074243```json44{45 "type": "https://api.example.com/problems/validation-error",46 "title": "Validation Error",47 "status": 400,48 "detail": "Request validation failed",49 "errors": [{"field": "email", "message": "must be a valid email"}]50}51```5253Enable: `spring.mvc.problemdetail.enabled: true`. Use `@RestControllerAdvice` with `ProblemDetail`.5455## Pagination5657| Type | Best For | Notes |58|------|----------|-------|59| Offset (`page=0&size=20`) | Admin UIs | Simple; slow at large offsets |60| Cursor (opaque token) | Feeds, infinite scroll | Consistent; no drift |61| Keyset (`afterId=X`) | Large datasets | Fastest; needs composite index |6263Cap `size` at 100. Prefer cursor/keyset.6465## Versioning6667Version in method path: `/api/{resource}/{version}/...`. v1 is forever — backward compatible. Optional fields for minor changes. Breaking changes = new version. Use `Sunset` + `Deprecation` headers.6869## Validation & Rate Limiting7071- `@Valid` on all `@RequestBody`. Bean Validation: `@NotBlank`, `@Email`, `@Size`, `@NotNull`.72- Whitelist sort fields to prevent injection.73- Rate limit headers: `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset`, `Retry-After`.7475## Checklist7677- [ ] Plural nouns, kebab-case, max 2-level nesting78- [ ] Correct HTTP methods and status codes79- [ ] RFC 7807 Problem Details for errors80- [ ] `@Valid` on all `@RequestBody`81- [ ] Global `@RestControllerAdvice` exception handler82- [ ] Pagination on list endpoints (cursor preferred)83- [ ] Sort field whitelist84- [ ] API version in URL path85- [ ] `Location` header on 201 responses86- [ ] Rate limiting with standard headers87- [ ] OpenAPI docs (`@Operation`, `@ApiResponse`)88- [ ] Idempotency keys on mutation endpoints89- [ ] Bulk operations capped (max 100)90- [ ] Async ops use 202 + polling9192## References9394- **[references/design-patterns.md](references/design-patterns.md)** — Cursor/keyset pagination code, filtering/sorting, field selection, OpenAPI config95- **[references/operations-docs.md](references/operations-docs.md)** — File upload/download, idempotency, bulk operations, long-running operations9697## Related Skills9899- **summer-rest** — Summer Framework handler pattern, ResponseFactory, exception handling100- **spring-webflux-patterns** — Controller implementation patterns (MVC + WebFlux)101- **spring-security** — Rate limiting headers, CORS, authentication for APIs102- **architecture** — Hexagonal interface layer design for REST endpoints