我计划我的Spring项目可以处理多个请求表单,例如:
{
"notification": "order_creation_notification",
"data": {
"date": "22 Jan 2022"
}
}
和
{
"notification": "login_notification",
"data": {
"email": "johndoe@email.com",
"at": "LA, USA"
}
}
因此,我创建了我的控制器和请求主体,如下所示:
- 下面是我的控制器:
@PostMapping(value="/")
public void createNotification(@RequestBody NotificationRequest request) {
...
}
- 下面是我请求类:
第一个
但是,当我尝试使用此json主体执行POST时:
{
"notification": "login_notification",
"data": {
"email": "johndoe@email.com",
"at": "LA, USA"
}
}
我得到一个错误,它说:
JSON parse error: Cannot construct instance of `LoginNotification` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `LoginNotification` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 8, column: 13] (through reference chain: NotificationRequest[\"data\"])
1条答案
按热度按时间3zwjbxry1#
您的代码几乎是正确的。
只是不要在
OrderCreationNotification
中使用Date date
。使用字段
String date
,并以自定义方式将String
转换为Date
。