loic911 / Rest-api-doc

MIT License
21 stars 32 forks source link

Support multiple verbs #6

Closed hoangdt84 closed 10 years ago

hoangdt84 commented 10 years ago

All of my services can be used with POST or GET. How can I specify both POST and GET in RestApiVerb? Thanks.

stevben commented 10 years ago

Hi,

Can you provide an example of URL Mapping and your controller ? Your GET & POST url mappings leads to the same method in your controller ?

hoangdt84 commented 10 years ago

Hi, here you are:

class UrlMappings {

    static mappings = {
        "/"(controller: 'base', action: 'welcome')
        "/crossdomain.xml"(controller: 'base', action: 'crossdomain')
        "/backoffice/online.xhtml"(controller: 'session', action: 'online')

        "/$controller/$action?" {
            constraints {
                // apply constraints here
            }
        }

        "401"(controller: 'error', action: 'unauthorized')
        "403"(controller: 'error', action: 'forbidden')
        "404"(controller: 'error', action: 'notFound')
        "500"(controller: 'error', action: 'serverError')
    }
}
class AccountController extends BaseController {
    ....
    @Secured(['ROLE_ANONYMOUS'])
    def register() {
        .....
    }
    ....
}

I can use this services /account/register with both GET and POST

loic911 commented 10 years ago

Its not possible to add multiple verb on a method with url mapping "/$controller/$action?" rules. The only way to do that would be to add explicit rules. Something like this: "/account/online"(controller:"account"){ action = [GET:"register", POST: "register"] }

Does the method register the same thing with GET and POST?

stevben commented 10 years ago

As loic911 said, we suggest you to write two explicit methods in your controller.

/account/online(controller:"account"){
action = [GET:"register1", POST: "register2"]
}

You can then add documentation for each method (the first with GET and the second with POST). Your business code can therefore move into a service.

hoangdt84 commented 10 years ago

Hi,

Thanks for your answers. But both of your suggestion will add extra work. We have about 170 services and this number is increasing.

So I think I will use POST for default and mention GET in another document.

loic911 commented 10 years ago

A simple way to solve this with a script (groovy or any other language): -parse the file restapidoc.json -for each entry from "apis.methods", copy the entry to a list in memory and just change the verb attribute (apis.methods[i].verb) from GET to POST -Add all entries from the list (entries with POST verb) to the apis.methods from json

Then you just need to execute your script after each grails rest-api-doc execution. You may copy the scripts/RestApiDoc.groovy file from plugins to your own scripts directory and add the call to your script at the end of the file (or add your script if its groovy)