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"]
}
}
}
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
– Thiago
I will update the response showing you how an HTTP GET request is made on my system.
– cantoni
I understand, so if I say the method
GET
should call the functionshow
from my controller when typing the above url I must have access to all the right registered records?– Thiago
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.
– cantoni
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?
– Thiago
Here, I created a chat room, better discuss it there.
– cantoni
Let’s go continue this discussion in chat.
– cantoni