API平台中的JSON API格式

时间:2018-02-21 22:56:39

标签: symfony json-api api-platform.com

首先,我想实施JSON API。 我遵循tutorial on api platform,就像在示例中创建实体和响应就像

{
    "links": {
        "self": "/api/books"
    },
    "meta": {
        "totalItems": 1,
        "itemsPerPage": 30,
        "currentPage": 1
    },
    "data": [
        {
            "id": "/api/books/1",
            "type": "Book",
            "attributes": {
                "isbn": "9781782164104",
                "title": "Persistence in PHP with the Doctrine ORM",
                "description": "This book is designed for PHP developers and architects who want to modernize their skills through better understanding of Persistence and ORM.",
                "author": "Kévin Dunglas",
                "publicationDate": "2013-12-01T00:00:00+01:00",
                "_id": 1
            },
            "relationships": {
                "reviews": {
                    "data": [
                        {
                            "type": "Review",
                            "id": "/api/reviews/1"
                        }
                    ]
                }
            }
        }
    ]
}

我的api_platform.yaml配置

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    formats:
        jsonapi:
            mime_types: ['application/vnd.api+json']

所以我对id中提交的data有疑问。我得到格式为id的{​​{1}}字段,但我只想得到数字(字符串),就像in JSON API一样。那么是否有一些我想念的配置或者是实现这一目标的任何方式

1 个答案:

答案 0 :(得分:0)

讨论了here。 您需要使用Normalizer或创建自定义getter。