Grails:相同的URL映射到不同HTTP方法的不同操作

问题描述 投票:0回答:2

我正在使用Grails v3.2.9

在官方文档中,我发现了mapping to http methods的以下内容:

static mappings = {
   "/product/$id"(controller:"product", action: "update", method: "PUT")
}

但这还不够。我需要的是有一个映射,它映射到基于HTTP方法的不同操作(在同一个控制器中)。

任何的想法 ?

grails url-mapping http-method controller-action
2个回答
1
投票

添加URLMappings,如 -

"/product/api/v2/book" (controller: 'book') {
    action = [GET: 'show', POST: 'update']
}

此外,最好在控制器中添加方法约束 -

  static allowedMethods = [show: 'GET', update: 'POST']

0
投票

或者,如果您遵循REST控制器的方法命名约定...您可以逃脱:

"/product/$id" (resources:'product')

这里有一些很好的信息:http://mrhaki.blogspot.com/2013/11/grails-goodness-customize-resource.html

© www.soinside.com 2019 - 2024. All rights reserved.