cometta
cometta

Reputation: 35679

spring controller json receive json List

I do post below json contain

{"testListObject":[{"testText":"bbb","testDate":"02.01.2011 00:00:00.000"},{"testText":"aaa","testDate":"01.01.2011 00:00:00.000"}]}

in my spring controller i have

@RequestMapping(value = "/post/tester/", method = RequestMethod.POST)
 public @ResponseBody String postItinerary(@ModelAttribute("testListObject") TestList testList) throws IOException {


    System.out.println("1="+testList); //ok
    System.out.println("2="+testList.childListObject); //print null
}

Any idea why i getting null for List childListObject ?

my pojo look like below

    public class TestList (){

    public List<ChildObject> childListObject;

//get and set
    }


    public class ChildObject(){

    public String testText;
    public String testDate;
//get and set    
}

Upvotes: 2

Views: 5548

Answers (2)

Affe
Affe

Reputation: 47954

@ModelAttribute invokes the web data binder. It's looking for ordinary post method parameters (e.g., param key - "childListObject[0].testText" param value "bbb") to bind onto your object.
For deserializing JSON into an object, you want to use @RequestBody to invoke the serializer.

Also, your JSON doesn't seem to match the obect. You JSON is just an array without the wrapper object, so if you submitted that as your request, the method parameter would be just a List.

Upvotes: 5

Bence Olah
Bence Olah

Reputation: 684

Have you configured org.springframework.http.converter.json.MappingJacksonHttpMessageConverter in your setting xml?

Upvotes: 0

Related Questions