嵌套的RESTful资源 [英] nested RESTful resources
问题描述
我使用2.3中由Grails引入的对REST的支持。我的应用程序包含以下域类: 在 如果我导航到URL
$ p $ @Resource(formats = ['json','xml'])
class Sensor {
String name
static hasMany = [metrics:Metric]
}
@Resource(formats = ['json','xml'])
class公制{b
$ b字符串名称
字符串值
static belongsTo = [sensor:Sensor]
}
UrlMappings.groovy
中,我定义了以下嵌套的RESTful URL映射:
/ api / sensors(资源:'sensor'){
/ metrics :metric)
}
/ api / sensors / 1 / metrics
我希望响应显示与 Sensor 关联的所有
Metric
code>与ID 1,但实际上它返回所有公制
实例(最多10个限制)
Me tric
与特定传感器
实例关联的实例(没有实现我自己的控制器)?
max
参数)?
看起来并不那么简单。 :)如果运行此命令,我们可以得到一个生动的图片:
grails url-mapping-report
code>
查看
控制器:公制
| GET | / api / sensors / $ {sensorId} / metrics |操作:index |
| GET | / api / sensors / $ {sensorId} / metrics / create |操作:创建|
| POST | / api / sensors / $ {sensorId} / metrics |操作:保存|
| GET | / api / sensors / $ {sensorId} / metrics / $ {id} |操作:show |
| GET | / API /传感器/ $ {} sensorId /度/ $ {ID} /编辑|操作:编辑|
| PUT | / api / sensors / $ {sensorId} / metrics / $ {id} |行动:更新|
|删除| / api / sensors / $ {sensorId} / metrics / $ {id} |操作:删除|
所以,我们至少需要一个 MetricController
继承 RestfulController
并覆盖 index()
来对 Metric
和基于传感器
的返回列表,如下所示:
class MetricController extends RestfulController< Metric> {
static responseFormats = ['json','xml']
MetricController(){
super(公制)
}
@Override
def index(){
def sensorId = params.sensorId
响应Metric.where {
sensor.id == sensorId
} .list()
上面的变化会提供预期的结果(包括限制分页结果)为 / api / sensors / 1 / metrics
命中。
I'm using the support for REST introduced by Grails in 2.3. My app includes the following domain classes:
@Resource(formats=['json', 'xml'])
class Sensor {
String name
static hasMany = [metrics: Metric]
}
@Resource(formats=['json', 'xml'])
class Metric {
String name
String value
static belongsTo = [sensor: Sensor]
}
And in UrlMappings.groovy
I've defined the following nested RESTful URL mappings:
"/api/sensors"(resources: 'sensor') {
"/metrics"(resources: "metric")
}
If I navigate to the URL /api/sensors/1/metrics
I expect the response to show all Metric
instances associated with the Sensor
with ID 1, but in fact it returns all Metric
instances (up to a limit of 10)
- Is there a URL that will return only
Metric
instances associated with a particular Sensor
instance (without implementing my own controller)?
- Is there a way to override the default limit of 10 results (without adding a
max
parameter to the request)?
解决方案 Looks like it isn't that simple. :) We can get a vivid picture if this command is run:
grails url-mapping-report
to see
Controller: metric
| GET | /api/sensors/${sensorId}/metrics | Action: index |
| GET | /api/sensors/${sensorId}/metrics/create | Action: create |
| POST | /api/sensors/${sensorId}/metrics | Action: save |
| GET | /api/sensors/${sensorId}/metrics/${id} | Action: show |
| GET | /api/sensors/${sensorId}/metrics/${id}/edit| Action: edit |
| PUT | /api/sensors/${sensorId}/metrics/${id} | Action: update |
| DELETE | /api/sensors/${sensorId}/metrics/${id} | Action: delete |
So, we would at least need a MetricController
inheriting RestfulController
and override index()
to do an additional check for Metric
and return list based on Sensor
as shown below:
class MetricController extends RestfulController<Metric> {
static responseFormats = ['json', 'xml']
MetricController() {
super(Metric)
}
@Override
def index() {
def sensorId = params.sensorId
respond Metric.where {
sensor.id == sensorId
}.list()
}
}
Above changed would provide the expected result (including the restriction on paginated results) for /api/sensors/1/metrics
when hit.
这篇关于嵌套的RESTful资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!