gpt4 book ai didi

java - spring controller json 接收 json 列表

转载 作者:行者123 更新时间:2023-11-30 06:36:32 24 4
gpt4 key购买 nike

我在 json 下发帖

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

在我的 Spring Controller 中我有

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

最佳答案

@ModelAttribute 调用网络数据绑定(bind)器。它正在寻找普通的 post 方法参数(例如,param key - "childListObject[0].testText"param value "bbb")以绑定(bind)到你的对象上。
要将 JSON 反序列化为对象,您需要使用 @RequestBody 来调用序列化程序。

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

关于java - spring controller json 接收 json 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4760853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com