Grails:如何在 UrlMappings 中为整个组设置参数
Grails: how to set a parameter for the whole group in UrlMappings
我们的 Grails 2.5 网络应用程序有 REST API。
我们需要启动新版本的 API,因此在 UrlMappings.groovy
中创建了一个组,如下所示:
group ("/v2") {
"/documents"(resources: 'document') {
"/meta"(resource: 'documentInfo', includes: ['show', 'update'])
}
"/folders"(resources: 'folder')
apiVersion = 2
}
问题是控制器操作的 params
中没有定义参数 apiVersion
。
如果在每个资源中都像这样定义参数,则该参数设置正确:
group ("/v2") {
"/documents"(resources: 'document') {
"/meta"(resource: 'documentInfo', includes: ['show', 'update']) {
apiVersion = 2
}
apiVersion = 2
}
"/folders"(resources: 'folder') {
apiVersion = 2
}
}
如何在组级别正确定义参数?
解决方法是使用命名空间...虽然我尝试在组级别定义它不会奏效,但您可以试一试。但是在资源级别定义甚至适用于嵌套资源。
group ("/v2") {
"/documents"(resources: 'document', namespace:'v2') {
"/meta"(resource: 'documentInfo', includes: ['show', 'update'])
}
"/folders"(resources: 'folder', , namespace:'v2')
}
我们的 Grails 2.5 网络应用程序有 REST API。
我们需要启动新版本的 API,因此在 UrlMappings.groovy
中创建了一个组,如下所示:
group ("/v2") {
"/documents"(resources: 'document') {
"/meta"(resource: 'documentInfo', includes: ['show', 'update'])
}
"/folders"(resources: 'folder')
apiVersion = 2
}
问题是控制器操作的 params
中没有定义参数 apiVersion
。
如果在每个资源中都像这样定义参数,则该参数设置正确:
group ("/v2") {
"/documents"(resources: 'document') {
"/meta"(resource: 'documentInfo', includes: ['show', 'update']) {
apiVersion = 2
}
apiVersion = 2
}
"/folders"(resources: 'folder') {
apiVersion = 2
}
}
如何在组级别正确定义参数?
解决方法是使用命名空间...虽然我尝试在组级别定义它不会奏效,但您可以试一试。但是在资源级别定义甚至适用于嵌套资源。
group ("/v2") {
"/documents"(resources: 'document', namespace:'v2') {
"/meta"(resource: 'documentInfo', includes: ['show', 'update'])
}
"/folders"(resources: 'folder', , namespace:'v2')
}