Search code examples
grailsgroovygrails-controller

Error occurred initializing command object on succeeding command objects in controller


I have the defined following controller

class BookController {

  def book(BookCommand bookCommand,
           AnotherBookCommand bookCommand2) 
  {
    ....
  }

When I debugged it, binding is done on the bookCommand and when it's bookCommand2's turn for binding, it throws this exception

Error in object 'bookCommand2': codes []; arguments []; default message [Error occurred initializing command object [bookCommand2]. org.apache.groovy.json.internal.Exceptions$JsonInternalException: Wrapped Exception
 CAUSE java.io.IOException :: Stream closed]

If I try to switch the order of the parameters i.e.

class BookController {

  def book(AnotherBookCommand bookCommand2,
           BookCommand bookCommand) 
  {
    ....
  }

Binding is done on the bookCommand2 and binding of bookCommand throws the exception.

Error in object 'bookCommand': codes []; arguments []; default message [Error occurred initializing command object [bookCommand]. org.apache.groovy.json.internal.Exceptions$JsonInternalException: Wrapped Exception
 CAUSE java.io.IOException :: Stream closed]

Any idea what's happening here?


Solution

  • We do not support binding the request of the body to multiple command objects. One option you have is to aggregate them into 1 class with something like this:

    class SomeClass {
        BookCommand bookCommand
        BookCommand anotherBookCommand
    }
    

    Then have your controller action accept one of those and organize the body of the JSON accordingly.