[英]How to send list of objects through POST mapping using Postman?
I am using Postman to send a post request with the following controller (which doesn't work)我正在使用 Postman 发送带有以下控制器的 post 请求(这不起作用)
@RequestMapping(value="/updateData", method=RequestMethod.POST, consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
public int updateData(@RequestBody FriendInfoForm request);
and I want this type of payload to be accepted as a "request" object.我希望这种类型的有效载荷被接受为“请求”对象。
{
"name": "John",
"age": 27,
"friendList": [
{
"name": "Jack",
"age": 20
},
{
"name": "Jill",
"age": 21
}
]
}
and this is the FriendInfoForm
definition:这是FriendInfoForm
定义:
public class FriendInfoForm {
private String name;
private int age;
private ArrayList friendList;
(getters and setters)
}
When I try to send the post request, name
and age
get set correctly, but friendList
doesn't (it doesn't get set to anything).当我尝试发送帖子请求时, name
和age
设置正确,但friendList
没有(它没有设置为任何内容)。 I can guess why (has to do with the Arraylist use).我能猜到原因(与 Arraylist 的使用有关)。
How do I send it correctly?如何正确发送?
you should create POJO like below class:--您应该像下面的课程一样创建 POJO:--
public class FriendInfoForm {
private String name;
private int age;
@JsonProperty("friendList")
private List<FriendList> friendList = new ArrayList<>();;
(getters and setters)
}
public class FriendList{
private String name;
private int age;
(getters and setters)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.