Google 日历插入事件:JSON 通过@RequestBody 解析错误Spring

Google Calendar insert event : JSON parse error through @RequestBody Spring

我尝试向 Google 日历中插入一个事件。

如果我直接使用 Java 对象创建我的事件,使用 Java client,我没有问题。

我的问题是当我将 JSON 中的事件发送到我的控制器时:(内容类型是 application/json,我使用邮递员或命令行发送它)

我的JSON:

{
  "summary": "Google I/O 2015",
  "location": "800 Howard St., San Francisco, CA 94103",
  "description": "A chance to hear more about Google\"s developer products.",
  "start":{
    "dateTime":"2018-04-07T17:00:00.000-04:00"
  },
  "end":{
    "dateTime":"2018-04-07T18:00:00.000-04:00"
  },
  "attendees": [
    {"email": "lpage@example.com"},
    {"email": "sbrin@example.com"}
  ]
}

我的控制器:

@PostMapping(path = "/new/event/{calendarId}/{sendNotification}")
public ResponseEntity<?> newEvent(@NonNull @PathVariable String calendarId, 
    @NonNull @PathVariable boolean sendNotification,
    @NonNull @RequestBody Event event) {

  Event eventCreated = postEventService.postNewEvent(calendarId, sendNotification, event);

  return new ResponseEntity<>(eventCreated, HttpStatus.CREATED);
}

这是我的错误:

{
  "status": 400,
  "error": "Bad Request",
  "message": "JSON parse error: Can not set com.google.api.services.calendar.model.EventDateTime field com.google.api.services.calendar.model.Event.start to java.util.LinkedHashMap; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not set com.google.api.services.calendar.model.EventDateTime field com.google.api.services.calendar.model.Event.start to java.util.LinkedHashMap (through reference chain: com.google.api.services.calendar.model.Event[\"start\"])",
  "path": "/new/event/primary/true"
}

我的问题是完全相同的 JSON 在文档中的 Try this API 上完美运行。

在此先感谢您的帮助!

您可以通过创建自定义 JSON 反序列化器来解决此错误。在 Spring Boot 中,您可以使用 @JsonComponent 注释来做到这一点。