知识卡片
按媒体类型分派解析
客户端需要同时使用返回不同表示格式的服务时,不能把所有响应交给同一个未经区分的解析器。
客户端用Accept说明可处理格式,收到消息后检查Content-Type,将正文交给对应解析器;不支持的格式应停止正常业务处理并报告。
解析器把Collection+JSON、SIREN或HAL转成客户端统一内部模型,业务逻辑不用随外部格式重复实现。结构化格式到HTML的通用转换较简单;转换为领域对象还需profile或schema指导;只有普通JSON/XML且无可靠元数据时,只能维护更脆弱的定制映射。
关联:[[客户端绑定消息结构]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 超媒体客户端 · 查看原书依据
Whenever your client makes an API request, you should include an HTTP Accept header to indicate the structured format(s) your application “understands.” Then, when the server replies, you should check the Content-Type header to confirm which of your supported formats was returned. If the format returned is not one your client can handle, you should stop processing and report the problem.
参考来源
- 位置:《RESTful Web API Patterns and Practices Cookbook》第4章《Hypermedia Clients》(来源项目book-sources:extracted/RESTful Web API Patterns and Practices Cookbook Connecting and Orchestrating Microservices and Distributed Data (Mike Amundsen)/_epub-src/OEBPS/ch04.xhtml,配方4.6)
- 结论依据:原文用Content-Type分派Collection+JSON、SIREN和HAL解析器,并讨论外部消息到内部模型的翻译成本。
- 原始内容:Whenever your client makes an API request, you should include an HTTP Accept header to indicate the structured format(s) your application “understands.” Then, when the server replies, you should check the Content-Type header to confirm which of your supported formats was returned. If the format returned is not one your client can handle, you should stop processing and report the problem.