How to create POST, PUT and DELETE method in Grails application?

4

I have a Grails 2.4.2 application that I want to communicate with another application, and this interaction should occur through a Rest service provided by Grails. Today the way it is implemented I just inform .json at the end of the URL that it returns me the formatted data. However I would like to know how to handle these implementations to add the methods POST , PUT and DELETE .

I did a search on the official documentation , so I got to following encoding:

UrlMappings.groovy

class UrlMappings {

static mappings = {
    "/$controller/$action?/$id?(.$format)?"{
        constraints {
            // apply constraints here
        }
    }

    "/"(controller:"main")
    "500"(view:'/error')

    "/patrimonios"(resource:'Patrimonio')
    }
}

When I run the application and access the http://localhost:8080/Patrimonio/patrimonios link I have a blank page in return. I tested the POST and DELETE methods through the POSTER addon and nothing happened as well. So I would like to know how best to proceed to achieve the desired goal?

UrlMappings.groovy (EDITED)

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(controller:"main")
        "500"(view:'/error')

        "/api/patrimonio/"(controller: "PatrimonioController") {
            action = [GET: "get"]
        }

    }
}
    
asked by anonymous 18.08.2015 / 20:06

1 answer

2

Below is an example that I use in an application in Grails (2.2.5) which is a REST service consumed by another application, also developed Grails (2.5.0).

In URLMapping:

    "/api/client/$labcode/$cliCode"(controller: "client") {
        action = [GET: "get"]
    }

    "/api/client"(controller: "client") {
        action = [POST: "save", PUT:"update"]
    }

The 1st URLMapping specifies that any HTTP request of the GET type that arrives for / api / client / $ labcode / $ cliCode will be redirected to the get "from the" client "driver.

$ labcode and $ cliCode are parameters passed to the action.

Sample HTTP GET request that is captured by the 1st URL Mapping:

link

In this context, the application is called Service. The remainder is exactly in the pattern for capturing URL Mapping.

The second mapping specifies that any POST-type HTTP request for / api / client will be redirected to the " save " action of the client ". Already requests of type PUT are directed to the action " update " of the same controller.

In the question example, you can do this:

"/$controller/$action?/$id?(.$format)?" {
    [POST: "save", PUT:"update", DELETE:"delete"]
    constraints {
        // apply constraints here
    }
 }

In this case, you are specifying that POST, PUT and DELETE HTTP requests that obey the mapping rule will be directed to actions save, update and delete, respectively.

On the blank pages, it was identified by chat that a non-existent ID was being passed in the database.

    
18.08.2015 / 20:21