nested RESTful resources

grails, rest

Solution

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.

Problem

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)?

Original source