使用 jsonapi 的配置问题

我打算使用json:api这个规范的,根据其文档:

Client Responsibilities
Clients MUST send all JSON:API data in request documents with the header Content-Type: application/vnd.api+json without any media type parameters.

Clients that include the JSON:API media type in their Accept header MUST specify the media type there at least once without any media type parameters.

Clients MUST ignore any parameters for the application/vnd.api+json media type received in the Content-Type header of response documents.

Server Responsibilities
Servers MUST send all JSON:API data in response documents with the header Content-Type: application/vnd.api+json without any media type parameters.

Servers MUST respond with a 415 Unsupported Media Type status code if a request specifies the header Content-Type: application/vnd.api+json with any media type parameters.

Servers MUST respond with a 406 Not Acceptable status code if a request’s Accept header contains the JSON:API media type and all instances of that media type are modified with media type parameters.

也就是说Content-TypeAccept必须为application/vnd.api+json,那么是否就是说这里的配置API_STANDARDS_TREE应该为vnd,而API_SUBTYPE呢?这个怎么配置?是否在这里配置了之后我后端的header就可以为application/vnd.api+json,而不需要再做其他配置了呢?我看了《RESTful Web APIs》一书,醍醐灌顶,只是目前空有理论,也许还要跳一些坑才能认清这方面理论与实践的差异。

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5
liyu001989

这个头还涉及切换版本,就按 dingo 的来呗,

5年前 评论

@liyu001989 那就是说不用json:api? 我就是没弄明白如果用json:api应该怎么配置

5年前 评论
liyu001989

json api 包括的东西这么多,就因为一个 header 稍微不一样就不用了?

想用就可以用啊,重要的不是响应的数据结构吗,https://github.com/liyu001989/dingo-serial... 中间件设置 json_api,就可以用了。

5年前 评论

@liyu001989 不好意思,可能我没有表达清楚。并不是我不想用json:api。看了《RESTful Web APIs》一书,我很喜欢这个开放性的规范,毕竟这属于书上说的四个标准之一,而且是比较好的标准。只是在dingo这里,我不知道怎么才能将Accept配置成application/vnd.api+jsonjson:api的API版本号好像不放在媒体类型里。我做了如下配置:

API_STANDARDS_TREE=vnd
API_SUBTYPE=api
API_STRICT=true

严格模式,必须带上Accept,但是此时Accept必须为application/vnd.api.v1+json,里边要带上v1这个版本号。即便不设置严格模式,访问非默认的v2Accept也必须为application/vnd.api.v2+json

5年前 评论
liyu001989

你的意思就是 必须完全按照 json:api 来,accept 必须是 application/vnd.api+json。

那么想办法就别让 dingo 解析这个头,不然可能就报错了。要么就别用 dingo 了,你需要的只是 fractal 而已。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!