Search code examples
restgrailsurl-mappinggrails-2.5urlmappings.groovy

Grails: how to set a parameter for the whole group in UrlMappings


Our Grails 2.5 web application has REST API.

We need to start a new version of the API so a group was created in UrlMappings.groovy like this:

group ("/v2") {

   "/documents"(resources: 'document') {
     "/meta"(resource: 'documentInfo', includes: ['show', 'update']) 
   }

   "/folders"(resources: 'folder')

   apiVersion = 2
}

The thing is that parameter apiVersion is not defined in params in controller actions.

The parameter is properly set if it is defined in each of the resources like this:

group ("/v2") {

   "/documents"(resources: 'document') {
     "/meta"(resource: 'documentInfo', includes: ['show', 'update']) {
       apiVersion = 2
     }
     apiVersion = 2
   }

   "/folders"(resources: 'folder') {
     apiVersion = 2
   }

}

How can I properly define a parameter on the group level?


Solution

  • Workaround will be using namespace... although my attempt to define it at group level wont work but you can give a try. But defining at resource level works even for nested resources.

    group ("/v2") {
    
       "/documents"(resources: 'document', namespace:'v2') {
         "/meta"(resource: 'documentInfo', includes: ['show', 'update']) 
       }
    
       "/folders"(resources: 'folder', , namespace:'v2')
    }