本文介绍了spring controller json收到json List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在json下面发帖包含

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
}

我知道为什么我为List childListObject获取null?

Any idea why i getting null for List childListObject ?

我的pojo如下所示

    public class TestList (){

    public List<ChildObject> childListObject;

//get and set
    }


    public class ChildObject(){

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


推荐答案

@ModelAttribute 调用Web数据绑定器。它正在寻找普通的post方法参数(例如,param key - childListObject [0] .testTextparam valuebbb)绑定到你的对象。

为了将JSON反序列化为一个对象,你想要使用 @RequestBody 来调用序列化程序。

@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.

此外,您的JSON似乎与obect不匹配。你JSON只是一个没有包装器对象的数组,所以如果你把它作为你的请求提交,那么方法参数就只是一个List。

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.

这篇关于spring controller json收到json List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:05