restful-hateoas
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity RESTful HATEOAS Best Practices
社区RESTful HATEOAS最佳实践
Comprehensive guide to building REST APIs that reach the Glory of REST (Richardson Maturity Level 3) in Ruby on Rails. Contains 44 rules across 9 categories, ordered by the request/response lifecycle — from resource URI design through hypermedia link relations to API evolution.
这是一份在Ruby on Rails中构建达到REST巅峰(Richardson成熟度第3级)的REST API的综合指南。包含9个类别共44条规则,按请求/响应生命周期排序——从资源URI设计到超媒体链接关系,再到API演进。
When to Apply
适用场景
Reference these guidelines when:
- Designing new REST API endpoints and resource URIs
- Adding hypermedia controls (_links, affordances) to API responses
- Implementing content negotiation with HAL, JSON:API, or vendor media types
- Building paginated, filterable, sortable collection endpoints
- Reviewing APIs for proper HTTP method semantics and status codes
- Evolving APIs without breaking existing clients
在以下场景中参考本指南:
- 设计新的REST API端点和资源URI
- 为API响应添加超媒体控制(_links、功能入口)
- 基于HAL、JSON:API或供应商媒体类型实现内容协商
- 构建支持分页、过滤、排序的集合端点
- 评审API的HTTP方法语义和状态码是否规范
- 在不破坏现有客户端的前提下演进API
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Resource Modeling | CRITICAL | |
| 2 | HTTP Method Semantics | CRITICAL | |
| 3 | Hypermedia & Link Relations | CRITICAL | |
| 4 | Status Codes & Response Headers | HIGH | |
| 5 | Content Negotiation & Media Types | HIGH | |
| 6 | Collection Patterns | MEDIUM-HIGH | |
| 7 | Error Semantics | MEDIUM | |
| 8 | Caching & Conditional Requests | MEDIUM | |
| 9 | API Evolution | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 资源建模 | 关键 | |
| 2 | HTTP方法语义 | 关键 | |
| 3 | 超媒体与链接关系 | 关键 | |
| 4 | 状态码与响应头 | 高 | |
| 5 | 内容协商与媒体类型 | 高 | |
| 6 | 集合模式 | 中高 | |
| 7 | 错误语义 | 中 | |
| 8 | 缓存与条件请求 | 中 | |
| 9 | API演进 | 中低 | |
Quick Reference
快速参考
1. Resource Modeling (CRITICAL)
1. 资源建模(关键)
- - URIs must be nouns, not verbs
res-noun-based-uris - - Always use plural nouns for collections
res-plural-collection-uris - - Limit nested resources to max 2 levels
res-limit-nesting-depth - - Model business entities, not database tables
res-model-business-entities - - Use opaque identifiers, never auto-increment IDs
res-use-consistent-identifiers - - Express relationships as sub-resources
res-sub-resources-for-relationships
- - URI必须使用名词,而非动词
res-noun-based-uris - - 集合URI始终使用复数名词
res-plural-collection-uris - - 嵌套资源最多限制为2级
res-limit-nesting-depth - - 建模业务实体,而非数据库表
res-model-business-entities - - 使用不透明标识符,绝不使用自增ID
res-use-consistent-identifiers - - 将关系表示为子资源
res-sub-resources-for-relationships
2. HTTP Method Semantics (CRITICAL)
2. HTTP方法语义(关键)
- - Keep GET requests free of side effects
http-get-must-be-safe - - Return 201 Created with Location header from POST
http-post-for-creation - - Use PUT only for full resource replacement
http-put-for-full-replacement - - PATCH for partial updates with merge semantics
http-patch-for-partial-updates - - Ensure DELETE is idempotent
http-delete-is-idempotent - - Use HEAD for metadata without body transfer
http-head-for-metadata
- - GET请求不得产生副作用
http-get-must-be-safe - - POST请求返回201 Created状态码及Location头
http-post-for-creation - - 仅在完全替换资源时使用PUT
http-put-for-full-replacement - - 使用PATCH进行带合并语义的部分更新
http-patch-for-partial-updates - - 确保DELETE请求是幂等的
http-delete-is-idempotent - - 使用HEAD请求获取元数据,无需传输响应体
http-head-for-metadata
3. Hypermedia & Link Relations (CRITICAL)
3. 超媒体与链接关系(关键)
- - Include a self link in every resource
link-self-link-every-resource - - Link to related resources instead of foreign keys
link-related-resource-links - - Expose available actions as conditional links
link-action-affordances - - Use IANA-registered link relation types
link-standard-relation-types - - Provide a root API entry point
link-entry-point - - Use hypermedia links for pagination
link-pagination-links - - Choose between embedding and linking
link-embedded-vs-linked
- - 每个资源都必须包含self链接
link-self-link-every-resource - - 链接到关联资源,而非返回外键
link-related-resource-links - - 将可用操作暴露为条件链接
link-action-affordances - - 使用IANA注册的链接关系类型
link-standard-relation-types - - 提供API根入口点
link-entry-point - - 使用超媒体链接实现分页
link-pagination-links - - 在嵌入和链接之间做出合理选择
link-embedded-vs-linked
4. Status Codes & Response Headers (HIGH)
4. 状态码与响应头(高)
- - Return 201 Created with Location header
status-201-with-location - - Return 204 No Content for empty responses
status-204-for-no-content - - Return 409 Conflict for state conflicts
status-409-for-conflicts - - Return 202 Accepted for async operations
status-202-for-async - - Return 405 with Allow header for wrong methods
status-allow-header-on-405
- - 返回201 Created状态码及Location头
status-201-with-location - - 空响应返回204 No Content状态码
status-204-for-no-content - - 状态冲突时返回409 Conflict状态码
status-409-for-conflicts - - 异步操作返回202 Accepted状态码
status-202-for-async - - 方法错误返回405状态码时需附带Allow头
status-allow-header-on-405
5. Content Negotiation & Media Types (HIGH)
5. 内容协商与媒体类型(高)
- - Respect the Accept header for content negotiation
media-accept-header-negotiation - - Set the correct Content-Type in every response
media-content-type-in-responses - - Use vendor media types for API versioning
media-vendor-media-types - - Return 406 for unsupported media types
media-406-for-unsupported-types
- - 尊重Accept头进行内容协商
media-accept-header-negotiation - - 每个响应都需设置正确的Content-Type
media-content-type-in-responses - - 使用供应商媒体类型进行API版本控制
media-vendor-media-types - - 不支持的媒体类型返回406状态码
media-406-for-unsupported-types
6. Collection Patterns (MEDIUM-HIGH)
6. 集合模式(中高)
- - Use cursor-based pagination instead of offset
coll-cursor-pagination - - Include pagination links in body and Link header
coll-link-header-pagination - - Support filtering via typed query parameters
coll-filtering-via-query-params - - Support sorting with a standardized sort parameter
coll-sorting-convention - - Support sparse fieldsets via fields parameter
coll-field-selection
- - 使用基于游标分页替代偏移量分页
coll-cursor-pagination - - 在响应体和Link头中包含分页链接
coll-link-header-pagination - - 支持通过类型化查询参数进行过滤
coll-filtering-via-query-params - - 使用标准化的sort参数支持排序
coll-sorting-convention - - 通过fields参数支持稀疏字段集
coll-field-selection
7. Error Semantics (MEDIUM)
7. 错误语义(中)
- - Use Problem Details (RFC 9457) for errors
err-problem-details - - Return structured validation errors
err-validation-errors - - Include recovery links in error responses
err-error-links - - Use machine-readable error codes
err-machine-readable-codes
- - 使用Problem Details(RFC 9457)格式返回错误
err-problem-details - - 返回结构化的验证错误
err-validation-errors - - 在错误响应中包含恢复链接
err-error-links - - 使用机器可读的错误码
err-machine-readable-codes
8. Caching & Conditional Requests (MEDIUM)
8. 缓存与条件请求(中)
- - Use ETags with stale? for conditional GET
cache-etag-conditional-get - - Set Last-Modified for time-based validation
cache-last-modified - - Set explicit Cache-Control headers
cache-cache-control-headers - - Include Vary header for content-dependent caching
cache-vary-header
- - 将ETag与stale?配合使用实现条件GET
cache-etag-conditional-get - - 设置Last-Modified头实现基于时间的验证
cache-last-modified - - 设置明确的Cache-Control头
cache-cache-control-headers - - 包含Vary头以支持基于内容的缓存
cache-vary-header
9. API Evolution (LOW-MEDIUM)
9. API演进(中低)
- - Make only additive changes to responses
evolve-additive-changes-only - - Use Deprecation and Sunset headers
evolve-deprecation-headers - - Leverage HATEOAS to eliminate URL versioning
evolve-hateoas-reduces-versioning
- - 仅对响应进行增量变更
evolve-additive-changes-only - - 使用Deprecation和Sunset头
evolve-deprecation-headers - - 利用HATEOAS消除URL版本控制
evolve-hateoas-reduces-versioning
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本和参考信息 |