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 particularSensor
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:
to see
So, we would at least need a
MetricController
inheritingRestfulController
and overrideindex()
to do an additional check forMetric
and return list based onSensor
as shown below:Above changed would provide the expected result (including the restriction on paginated results) for
/api/sensors/1/metrics
when hit.