Grails REST API - 使用params中的分页,元数据和自定义包含字段呈现JSON

Ibr*_*him 12 rest grails json

我在寻找设计基于此演示文稿Grails的REST风格的API自定义JSON响应一个最好的方式设计美观REST + JSON API的莱斯黑兹尔伍德.

这是我的Domain类

class TaxiType {

    Date dateCreated, lastUpdated
    String description
    User createdBy

    static hasMany = [taxis: Taxi]

    static constraints = {
    }
}
Run Code Online (Sandbox Code Playgroud)

列表所需的响应格式是

{
  "meta": {
    "href": "https://api.mydomain.com/taxi-types",
    "otherData": "..."
  },
  "paging": {
    "offset": 0,
    "limit": 10,
    "total": 100,
    "first": "https://api.mydomain.com/taxi-types?offset=0&limit=10",
    "previous": null,
    "next": "https://api.mydomain.com/taxi-types?offset=90&limit=10",
    "last": "https://api.mydomain.com/taxi-types?offset=90&limit=10"
  },
  "data": [
    {
      "href": "https://api.mydomain.com/taxi-types/1",
      "id": 1,
      "description": "description 1",
      "taxis": {
        "href": "https://api.mydomain.com/taxi-types/1/taxis"
      }
    },
    ...
  ]
}
Run Code Online (Sandbox Code Playgroud)

TaxiTypeController.groovy

def index(Integer limit) {
    params.max = Math.min(limit ?  : 10, 100)
        params.offset = params ? .offset ? .toInteger()
        withFormat {
        json {
            respond TaxiType.list(params),
            [includes : includeFields,
                paging : [total : TaxiType.count(), limit : params ? .max, offset : params ? .offset ?  : 0]
            ]
        }
    }
}
private getIncludeFields() {
    params.fields ? .tokenize(', ')
}
Run Code Online (Sandbox Code Playgroud)

SumoJsonCollectionRenderer.groovy

class SumoJsonCollectionRenderer extends JsonCollectionRenderer {
    SumoJsonCollectionRenderer(Class componentType) {
        super(componentType)
    }
    public SumoJsonCollectionRenderer(Class componentType, MimeType...mimeTypes) {
        super(componentType, mimeTypes)
    }
     @ CompileStatic(SKIP)
     @ Override
    protected void renderJson(object, RenderContext context) {
        log.debug(object)
        log.debug(object.size())
        log.debug(object.getTotalCount())
        Map tObject = ['data' : object]
        if (context.arguments ? .paging) {
            tObject['paging'] = context ? .arguments ? .paging
        }
        super.renderJson(tObject, context)
    }
}
Run Code Online (Sandbox Code Playgroud)

所需功能包括:

1)API用户应该只能获得必填字段(部分表示)

GET https://api.mydomain.com/taxi-types??fields=id,description,taxis
Run Code Online (Sandbox Code Playgroud)

对此请求的期望响应应该是

{
    "meta" : {...}
    "paging" : {...}
    "data" : [{
            "href" : "https://api.mydomain.com/taxi-types/1",
            "id" : 1,
            "description" : "Taxi Type1",
            "taxis" : [{
                    "href" : "https://api.mydomain.com/taxis/123"
                },
                ...
            ]
        },
        ...
    ]
}
Run Code Online (Sandbox Code Playgroud)

我得到的是

{
    "data" : [{
            "id" : 1,
            "description" : "Taxi Type1",
            "taxis" : [{
                    "class" : "com.domain.project.Taxi",
                    "id" : 1
                }
            ]
        },
        ...
    ],
    "paging" : {
        "total" : 80,
        "limit" : 10,
        "offset" : 0
    }
}
Run Code Online (Sandbox Code Playgroud)

我提到了这个问题的答案,从Grails RestfulController索引/搜索操作渲染分页元数据.

但是仍然需要包含first, previous, next & last上面所需格式中提到的分页链接.

2)自定义输出

例如,taxis具有hasMany关系的属性应默认呈现为

"taxis": {
    "href": "https://api.mydomain.com/taxis/12345"
}
Run Code Online (Sandbox Code Playgroud)

如果用户更喜欢扩展taxis属性,例如:GET /taxi-types?expand=taxis,JSON格式应该是

"taxis": {
    "href": "https://api.mydomain.com/taxis/12345"
    "name": "Taxi name",
    "type": "https://api.mydomain.com/taxi-types/1"
    ...
}
Run Code Online (Sandbox Code Playgroud)

3)将meta对象添加到所有响应

"meta": {
     "href": "https://api.mydomain.com/taxi-types",
     ...
}
Run Code Online (Sandbox Code Playgroud)

我试过Json Marshaller来定制响应.这是我的Marshaller代码

JSON.registerObjectMarshaller(TaxiType) {TaxiType taxiType->
    return [
        id : taxiType ? .id,
        description : taxiType ? .description
    ]
}
Run Code Online (Sandbox Code Playgroud)

但它总是在返回数组中使用这两个属性呈现id & description,即使我想要一些其他属性taxis.

如何实施以上3项要求?提前致谢.

Eja*_*med 0

我为 grails 3创建了一个插件,它允许您向 api 添加分页、自定义元数据和其他有用的功能。您可以使用 LinkGenerators 以及我的插件添加 HAL 功能。