且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

嵌套的RESTful资源

更新时间:2023-11-20 15:16:22

看起来并不那么简单。 :)如果运行此命令,我们可以得到一个生动的图片:

  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.