[英]How to send in postman Map with key as object?
I have such an entity structure so I want to send a post request and create Order with all properties.我有这样一个实体结构,所以我想发送一个发布请求并创建具有所有属性的订单。 How to request body should look like in postman?
如何请求正文在 postman 中应该是什么样子?
@Entity
public class Order {
@Id int id;
String name;
@ElementCollection
@CollectionTable(name="INVENTORY", joinColumns=@JoinColumn(name="STORE"))
@Column(name="COPIES_IN_STOCK")
@MapKeyJoinColumn(name="MOVIE", referencedColumnName="ID")
Map<Item, Integer> items;
}
@Entity
public class Item {
@Id long id;
String name;
}
@RestController
@RequestMapping("api/v1/order")
public class OrderController {
public final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
@PostMapping
public void saveOrder(@RequestBody Order order){
orderService.save(order);
}
}
this is the wrong body, so I try to find out how it should look according to my data model这是错误的身体,所以我试着根据我的数据找出它应该是什么样子 model
{
"name":"first order",
"items": {
"item-1":{
"name": "frist item"
},
"item-2":{
"name": "second item"
}
}
}
expect to get the status to receive from the client Order with the whole structure期望获得状态以从具有整个结构的客户订单接收
example from here data what I got screenshot来自这里的示例数据我得到的截图
I think @RequestBody
by default cannot handle this example request:我认为
@RequestBody
默认情况下无法处理此示例请求:
{
"name":"first order",
"items": {
"item-1":{
"name": "frist item"
},
"item-2":{
"name": "second item"
}
}
}
For that you should try something like this:为此你应该尝试这样的事情:
@Entity
public class Order {
@Id int id;
String name;
@ElementCollection
@CollectionTable(name="INVENTORY", joinColumns=@JoinColumn(name="STORE"))
@Column(name="COPIES_IN_STOCK")
@MapKeyJoinColumn(name="MOVIE", referencedColumnName="ID")
Map<Item, Integer> items;
@JsonAnySetter
public void addItems(String key, Item item) {
items.put(item, Integer.valueOf(key.split("-")));
}
}
If you want to count the item's repetition number as a key in the hashmap put your logic in this setter如果您想将项目的重复次数计为 hashmap 中的键,请将您的逻辑放入此设置器中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.