How do you properly bind a list of objects into a Grails command?

9k views Asked by At

I'm trying to figure out how to deserialize and validate nested objects in JSON request into a Grails 2.1.1 command object.

Currently I have a command object in my controller that has a few basic properties and then list of domain objects,

protected static class CustomCommand {
    String name
    String description
    List<DomainObject> objs
}

And a JSON body to my POST request,

{
    name: 'test name',
    description: 'test description',
    objs: [
        {
            name: 'test sub object',
            description: 'test description'
        }
    ]
}

I'm seeing the command object created with an empty array. Any idea how I can get the sub objects my in JSON body to deserialize into the command object and then validate them?

Previously I've worked around this by manually creating an object from the parameter map and validating that directly, but that feels like a workaround that isn't taking advantage of everything Grails offers.

2

There are 2 answers

0
James McMahon On BEST ANSWER

I never got this to work under Grails 2.1.1, but apparently this was fixed in Grails 2.3,

Binding Request Body To Command Objects If a request is made to a controller action which accepts a command object and the request includes a body, the body will be parsed and used to do data binding to the command object. This simplifies use cases where a request includes a JSON or XML body (for example) that can be bound to a command object. See the Command Objects documentation for more details.

4
micha On

We had a similiar issue with binding post data to a list inside a command. Our workaround that worked was to define a default value for collection elements:

class MyCommand {    
    List<MyClass> items= [].withLazyDefault {
        new MyClass()
    }
}

After that the post data was correctly bound to the list. I think the reason is that Groovy ignores the generic type parameter of the list and does not know which object to instantiate at runtime.

I'am not sure if this works in your case but it might be worth a try

Update:

I used this a few minutes ago:

public static class MyCommand {
    String foo
    List<Bar> bars

    public String toString() {
        return "foo: " + foo + ", bars: " + bars
    }
}

public static class Bar {
    String baz
}

controller method:

def test() {
     println new MyCommand(request.JSON)
}

I posted some json using jquery:

$.ajax({
    type: "POST",
    url: '...',
    data: JSON.stringify({ 
        'foo': '12345', 
        bars: [
            {baz: '1'}, 
            {baz: '2'}
        ]
    }),
    contentType : 'application/json',
});

The output printed by the controller:

foo: 12345, bars: [[baz:1], [baz:2]]

So it seems to work :o