0

In Grails 2.5.0, is it possible to inject a property value from a JSON POST body into a controller action method parameter that isn't a command object? e.g., into a String, a primitive, etc.

This was possible in Grails 2.2.4, but I haven't found a way to do it in 2.5.0.

(I know that query string values can be injected into controller action method parameters in both Grails 2.5.0 & 2.2.4)

XDR
  • 4,070
  • 3
  • 30
  • 54

1 Answers1

0

See Binding Request Body To Command Objects section in http://grails.github.io/grails-doc/2.3.0/guide/introduction.html#whatsNew23. It changed a bit in Grails 2.3.x. Basically if you try to access the request JSON two times it won't be available to you since Grails closes the request stream after parsing the data and uses it to bind any CommandObject or any domain instance (as command object).

So if you are passing request JSON to a action say support: {"foo": "bar"} and you are trying to do this:

class SomeController {

    def test(String foo) {
        println foo        // Will be null
        println request.JSON.foo           // Will be "bar"
    }
}

Instead any domain class binding will work now:

class MyDomainClass {

     String foo
}

And modified controller action:

class SomeController {

    def test(MyDomainClass domainInstance) {
        println domainInstance.foo        // Will be "bar"
        println request.JSON           // Will be null since request stream is closed and binded to the domainInstance
    }
}
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • Thanks for the info. It sounds like Grails 2.3+ cannot inject non-command objects into action methods. I should have mentioned that I already knew that I could use a command object or `request.JSON` to get JSON HTTP body properties. I wanted to not have to look through every action method in every controller, and then have to change the code in every affected method to pass through String / primitive HTTP body data (since both command objects & `request.JSON` require code changes). – XDR Jul 04 '15 at 16:27
  • Oh, so you need some alternative code to backward support your code since it is not supported in Grails 2.3+? – Shashank Agrawal Jul 04 '15 at 18:10
  • I don't have to have an alternative, but it would be nice to avoid looking through scores of controllers and hundreds of their action methods that I didn't write to determine which arguments are from query strings, and which are from HTTP bodies. – XDR Jul 05 '15 at 03:51