【发布时间】:2020-11-13 14:07:12
【问题描述】:
我正在使用 Postman 通过以下控制器发送发布请求(不起作用)
@RequestMapping(value="/updateData", method=RequestMethod.POST, consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
public int updateData(@RequestBody FriendInfoForm request);
我希望这种类型的有效负载被接受为“请求”对象。
{
"name": "John",
"age": 27,
"friendList": [
{
"name": "Jack",
"age": 20
},
{
"name": "Jill",
"age": 21
}
]
}
这是FriendInfoForm的定义:
public class FriendInfoForm {
private String name;
private int age;
private ArrayList friendList;
(getters and setters)
}
当我尝试发送 post 请求时,name 和 age 设置正确,但 friendList 没有(它没有设置任何内容)。我可以猜到原因(与 Arraylist 的使用有关)。
如何正确发送?
【问题讨论】:
-
您应该创建一个具有名称和年龄属性的
Friend类型,然后将FriendInfoForm.friendList的类型更改为ArrayList<Friend>。 -
明白了,谢谢!有时简单的解决方案是最好的