我正在寻找一种最佳方法,根据此演示文稿 Design Beautiful REST + JSON APIs 在 Grails 中为 RESTful API 设计自定义 JSON 响应通过 Les Hazlewood .
这是我的域类
class TaxiType {
Date dateCreated, lastUpdated
String description
User createdBy
static hasMany = [taxis: Taxi]
static constraints = {
}
}
列表所需的响应格式是
{
"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"
}
},
...
]
}
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(', ')
}
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)
}
}
所需的功能是:
1) API 用户应该只能获取必填字段(部分表示)
GET https://api.mydomain.com/taxi-types??fields=id,description,taxis
此请求所需的响应应该是
{
"meta" : {...}
"paging" : {...}
"data" : [{
"href" : "https://api.mydomain.com/taxi-types/1",
"id" : 1,
"description" : "Taxi Type1",
"taxis" : [{
"href" : "https://api.mydomain.com/taxis/123"
},
...
]
},
...
]
}
我得到的是
{
"data" : [{
"id" : 1,
"description" : "Taxi Type1",
"taxis" : [{
"class" : "com.domain.project.Taxi",
"id" : 1
}
]
},
...
],
"paging" : {
"total" : 80,
"limit" : 10,
"offset" : 0
}
}
我提到了这个问题的答案Render metadata for pagination from Grails RestfulController index/search actions .
但仍然需要在分页中包含第一个、上一个、下一个和最后一个
的链接,如上面所需格式中所述。
2) 自定义输出
例如
具有 hasMany
关系的 taxis
属性默认应呈现为
"taxis": {
"href": "https://api.mydomain.com/taxis/12345"
}
如果用户喜欢扩展 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"
...
}
3) 将meta
对象添加到所有响应
"meta": {
"href": "https://api.mydomain.com/taxi-types",
...
}
我尝试使用 Json Marshaller 来自定义响应。这是我的编码器代码
JSON.registerObjectMarshaller(TaxiType) {TaxiType taxiType->
return [
id : taxiType ? .id,
description : taxiType ? .description
]
}
但它总是使用返回数组 id 和 description
中的这两个属性进行渲染,即使我想要一些其他属性,例如 taxis
。
以上3个要求如何实现?提前致谢。
最佳答案
我创建了一个 plugin for grails 3它允许您向 api 添加分页、自定义元数据和其他有用的功能。您可以使用 LinkGenerators 以及我的插件添加 HAL 功能。
关于json - Grails REST API - 使用分页、元数据和来自参数的自定义包含字段渲染 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26635463/