I'm trying to implement a simple RestfulController for my application. Given the following domain class:
class Test {
String name
int someInteger
static constraints = {
}
}
and its controller:
class TestController extends RestfulController<Test>{
TestController() {
super(Test)
}
}
Inside conf/UrlMappings.groovy I added the following entries:
"/api/$controller?(.${format})?" {
action = [POST: "save", PUT: "save", GET: "index", DELETE:"error"]
}
"/api/$controller/$id?(.${format})?" {
action = [POST: "update", PUT: "update", GET: "show", DELETE: "delete"]
}
Get requests are working fine, but Post and Put requests to a URL like http://localhost:8080/app/api/test.json when the Content-Type: application/x-www-form-urlencoded Header is present fail to respond with a JSON as expected. Instead render the show action view after persisting the entrie sent.
I also tried to use the Header Accept: application/json with no effect.
How can I fix that?
Edit:
Further investigating RestfulController's source file and the docs section regarding Content Negotiation I was able fix it by overriding the save and update methods replacing the line:
request.withFormat {
with:
withFormat {
Is it intentional or is there a flaw on RestfulController's implementation?
Why does it consider the Content-Type header instead of the Accept header to render response?