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

Asked

Viewed 799 times

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 as it is implemented just I inform . json at the end of the URL that it returns the formatted data. However I would like to know how to manipulate these implementations in order to add the methods POST, PUT and DELETE.

I did a search on official documentation, with that I arrived at the 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 link http://localhost:8080/Patrimonio/patrimonios I got a blank page coming back. I tested the POST and DELETE methods through the POSTER addon and nothing happened either. Therefore, 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"]
        }

    }
}

1 answer

2


Below is an example that I use in a Grails application (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"]
    }

1st Urlmapping specifies that any HTTP request of the type GET that arrive to /api/client/$labcode/$cliCode will be redirected to the action "get" of the controller "client".

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

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

https://localhost:8443/Service/api/client/BRMIN01/CL-000223

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

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

In the example of the question, one can do this:

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

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

On the blank pages, has been identified by chat that a non-existent ID was being passed into the database.

  • I made the modification as suggested (coding in the first post), just fitting for the project here. When trying to access the link http://localhost:8080/Patrimonio/patrimonio/api/patrimonio I have as return HTTP Status 404 - /Patrimonio/patrimonio/api/patrimonio

  • I will update the response showing you how an HTTP GET request is made on my system.

  • I understand, so if I say the method GET should call the function show from my controller when typing the above url I must have access to all the right registered records?

  • You see, in this case, I believe the show you’re referring to has a GSP of the same name, correct? Note that the system I am demonstrating is Restful (or at least almost). I do not have Gsps in this application. The only thing it does is to serve with XML or JSON the applications that want to consume it. If your application has both roles, that is, a normal application with Gsps and another role as a Restful API, then I suggest you divide as shown above. Everything that comes after /api/ is Rest. Everything that comes without /api/ is a normal application, with Gsps, etc.

  • I have a Grails application that I want to communicate with another developed in Android. Today I can already consume the Android application through the Grails application. I access the . json of each controller of the Grails application. What I would like is to access the save, update and delete methods that are in the Grails controller by Android. Sorry if I expressed bad before. So it’s better if I create a separate controller only for the Rest functions?

  • Here, I created a chat room, better discuss it there.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.