Grails urlmappings, maybe one urlmappings prefix?

So, for example, let's say I have an API in webapp, and I want to use the same controllers and actions in the API as other web applications.

In my urlmappings file I have

"/api/$version/$apiKey/$controller/$acion/$id?"

and I also have a mapping like this:

"/blog/$year/$month/$day/$action" {
   controller = 'blog'
 }

Now the question is, can I somehow prefix the api urlmapping in the urlmapping of the blog so that I can use the variables $ year, $ month, $ day? so that a GET request is given to the following URL:

GET /api/0.1/bs23mk4m2n4k/blog/2001/01/05/list

or am I forced to make the following request instead?

GET /api/0.1/bs23mk4m2n4k/blog/list?year=2004&month=01&day=05

Need help from urlmappings GURU or groovy manlupings runtime runtime WIZARD :)

I want a solution that can reuse existing URLs other than api, instead of redrawing them using the api path as a prefix.

+3
3

ApiController api, . :

"/api/$version/$apiKey/$rest**" {
     controller:'api'
     action:'default'
}


import org.codehaus.groovy.grails.web.util.WebUtils
class ApiController {
    def grailsUrlMappingsHolder

    def default = {
        // validate apiKey, etc
        WebUtils.forwardRequestForUrlMappingInfo(request, response, grailsUrlMappingsHolder.match("/${params.rest}"))
    }
}

API apiKey , UrlMapping.

+2

, . url: http://grails.org/doc/latest/guide/single.html#6.4 URL

:

static mappings = {
   "/api/$version/$apiKey/$controller/$year/$month/$day/$action"()
} 

URL-, :

http://localhost:8080/api/0.1/bs23mk4m2n4k/blog/2001/01/05/list

. params url ( ).

.

params.version
0

, URL-: http://www.grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.4.9%20Named%20URL%20Mappings

, !

urlMapping:

name blogWithYear:"/api/$version/$apiKey/$controller/$year/$month/$day":{
controller = 'blog'
action = 'youraction'
}

<g:link mapping="blogWithYear" params="[$version:'0.1', ....., '$year: 2011']">
Show blog
</g:link>

With g: link you can now compile the url, but you want by adding parameters.

-1
source

All Articles