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

时间:2018-04-06 09:56:01

标签: java google-calendar-api

我尝试将事件插入Google日历。

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

我的问题是当我将我的JSON事件发送给我的控制器时:( Content-type是application / json,我使用postman或命令行发送它)

我的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上完美运行。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以通过创建自定义JSON解串器来解决此错误。在Spring Boot中,您可以使用@JsonComponent批注进行此操作。