我需要反序列化此JSON:
{
"order":{
"address": "Adress XXX",
"status": "Open",
"confirmationDates": "2019-04-03"
},
"orderItems": [
{
"description": "Item 1",
"unitPrice": 10.5,
"quantity": 2
},
{
"description": "Item 2",
"unitPrice": 22.5,
"quantity": 2
},
{
"description": "Item 3",
"unitPrice": 10000.5,
"quantity": 2
}
]
}
我正在使用Spring Boot,并在此正文中有一个Post请求。
@PostMapping("/orderwithitems")
public ResponseEntity<Object> addOrderWithItems(@RequestBody OrderOrderItem orderOrderItem){...}
包装类是这个POJO:
public class OrderOrderItem{
private Order order;
private List<OrderItem> orderItems;
public OrderOrderItem(){
orderItems = new ArrayList<>();
order = new Order();
}
public OrderOrderItem(Order order, List<OrderItem> orderItems) {
this.order = order;
this.orderItems = orderItems;
}
...
}
问题是当我发帖时出现此错误:
{
"timestamp": "2019-04-04T23:04:19.640+0000",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot construct instance of `com.invillia.acme.OrderController$OrderOrderItem` (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.invillia.acme.OrderController$OrderOrderItem` (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor\n at [Source: (PushbackInputStream); line: 2, column: 2]",
"path": "/orderwithitems"
}
我正在尝试遵循以下示例: https://www.baeldung.com/jackson-collection-array
https://www.baeldung.com/spring-rest-template-list
但是我找不到解决方法。
答案 0 :(得分:1)
问题是我将“ OrderOrderItem”声明为控制器的内部类。 将OrderOrderItem移到新文件可使事情正常运行。