dimanche 19 avril 2015

Grails 3 and Json: "Request method 'POST' not supported"

I'm porting a working webapp from Grails 2.3 to 3.0.1. When I post the Json string {"command":"ping"} to the server i get the following result:



{"timestamp":1429380750958,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/rps/request"}


Here's the controller:



import org.grails.web.json.JSONObject
class RequestController {
def jsonManagerService
def index() {
JSONObject json = request.JSON
if(!json){
render "{json or gtfo}"
return
}
render jsonManagerService.parseJson(json)
}
}


Here's the JsonManagerService:



import grails.transaction.Transactional
import org.grails.web.json.JSONObject
@Transactional
class JsonManagerService {
def parseJson(JSONObject o) {
switch (o.command){
case("ping"):
return '{"result":"pong"}'
break;
default:
return '{"result":"unknown command"}'
}
}
}


And here's my UrlMappings.groovy (it's the default one):



class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
"404"(view:'/notFound')
}
}


It looks like a Spring related issue. All the search on this matter provided no results. Any idea?


Edit: thanks @dmahapatro, added UrlMappingsgroovy. Corrected the controller, dumb mistake, but result is still the same.


Aucun commentaire:

Enregistrer un commentaire