aveltens
aveltens

Reputation: 323

Gerneric url mapping for restful resources in grails

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?

Upvotes: 2

Views: 2524

Answers (2)

Greg
Greg

Reputation: 116

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 )            
        } ...

Upvotes: 2

Jon Cram
Jon Cram

Reputation: 17309

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.

Upvotes: 0

Related Questions