【问题标题】:spring controller json receive json Listspring控制器json接收json列表
【发布时间】:2011-06-13 05:59:18
【问题描述】:

我确实在 json 包含下面发帖

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

在我的弹簧控制器中,我有

@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 吗?

我的 pojo 如下所示

    public class TestList (){

    public List<ChildObject> childListObject;

//get and set
    }


    public class ChildObject(){

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

【问题讨论】:

    标签: java json spring rest jackson


    【解决方案1】:

    @ModelAttribute 调用 Web 数据绑定器。它正在寻找普通的 post 方法参数(例如,参数键 - “childListObject[0].testText”参数值“bbb”)以绑定到您的对象。
    要将 JSON 反序列化为对象,您需要使用 @RequestBody 来调用序列化程序。

    另外,您的 JSON 似乎与对象不匹配。您的 JSON 只是一个没有包装对象的数组,因此如果您将其作为请求提交,则方法参数将只是一个列表。

    【讨论】:

      【解决方案2】:

      你有没有在你的设置xml中配置org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

      【讨论】:

        猜你喜欢
        • 2014-05-25
        • 1970-01-01
        • 2023-03-19
        • 2017-02-25
        • 2014-06-29
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多