2

I have configured some restful resources in grails like this

"/book/$id?"(resource:"book")
"/author/$id?"(resource:"author")

But I want to to this more generic like

"/$controller/$id?"(resource: controller)

which doesn't work... (getting 404)

How can i configure a generic url mapping for restful resources in grails?

aveltens
  • 323
  • 2
  • 10

2 Answers2

2

It would seem that the "resource" portion of the mapping is evaluated on startup, not on execution of the actual request (a guess from playing around with this). Therefore, I think you need to "preload" the set of UrlMappings you want dynamically based on the Domain classes available on application startup. Something like this might do the trick:

class UrlMappings {
static mappings = {
        ApplicationHolder.application.getArtefacts('Domain').each { GrailsClass cl ->
           "/${cl.propertyName}/$id?"(resource: cl.propertyName )            
        } ...
Greg
  • 116
  • 3
  • 1
    your guess might be right, your solution works, thx. But it would be really neat, if grails evaluated the resource mapping per request, as in the controller / action mapping – aveltens May 27 '11 at 07:20
0

The correct mapping for your needs depends entirely on what you are trying to achieve (which can't be determined from the question).

Consider a generic mapping:

"/$controller/$action?/$id?"{}

This will map the first part of the URL to a controller of the same name, the second part to an action of that controller and the third part to what should be a unique identifier for a domain class instance.

Examples that work with this mapping:

/book/show/2

Controller: bookController
Action: show
id: 2

This will call the show action of the book controller.
params.id will be 2.

The intention implied in the URL is to show relevant details for a book object 
with an id of 2.

/question/update/6

Controller: questionController
Action: update
id: 6

This will call the update action of the question controller.
params.id will be 6.

The intention implied in the URL is to update the details for a question
object with an id of 6.
Jon Cram
  • 16,609
  • 24
  • 76
  • 107
  • Thanks, but I don't want to map to actions, but to resources as explained in the question and in 13.1 "REST" Grails User [Guide](http://grails.org/doc/latest/guide/13.%20Web%20Services.html#13.1%20REST). But I don't want to name all the resources (book, author etc.) explicitly, I want to do it in a generic way, as you explained for the actions (as you don't name the actions explicitly) – aveltens May 25 '11 at 18:45