JSON分析错误:无法从数组值反序列化“”类型的值

xvw2m8pv  于 2022-10-15  发布在  PostgreSQL
关注(0)|答案(1)|浏览(247)

我有一个对象Event(Long id,String name, LocalDateTime start, LocalDateTime stop, Desk desk(Long id, String name),我想用Create方法保存在数据库中:

@PostMapping("/flexoffice/events/create")
@ResponseStatus(HttpStatus.ACCEPTED)
public Event create(@RequestBody Event newEvent){
    // It checks if the event is available, if the office is not taken during the time interval. If it is not, it throws an exception.
    if (eventRepository.checkIfAvailable(newEvent.getDesk().getId(), newEvent.getStart(),newEvent.getStop()) == true) {

        throw new ResponseStatusException(HttpStatus.CONFLICT,"créneau en conflit avec un autre");
    }
    return this.eventRepository.save(newEvent);

}

当我尝试使用Json格式的RequestBody在Postman上执行Postmap时:

[{
"name": "Michellllllllllll",
"start": "2021-01-01T00:01:00",
"stop": "2021-03-06T00:01:00",
"desk": {
  "name": "B-1",
  "id": 9
}}]

它返回:

Hibernate: select desk0_.Id as id1_1_0_, desk0_.board_id as board_id3_1_0_, desk0_.name as name2_1_0_, board1_.id as id1_0_1_, board1_.name as name2_0_1_ from desk desk0_ left outer join board board1_ on desk0_.board_id=board1_.id where desk0_.Id=?
2022-10-14 10:37:04.814 
 WARN 19272 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `org.flexoffice.application.entity.Event` from Array value (token `JsonToken.START_ARRAY`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `org.flexoffice.application.entity.Event` from Array value (token `JsonToken.START_ARRAY`)<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]]

我已经尝试使用空类,然后以字符串和格式捕获所有请求体,等等,但没有任何变化。请帮帮我!

qzwqbdag

qzwqbdag1#

尝试将此JSON作为请求正文:

{
   "name":"Michellllllllllll",
   "start":"2021-01-01T00:01:00",
   "stop":"2021-03-06T00:01:00",
   "desk":{
      "name":"B-1",
      "id":9
   }
}

并且不建议在客户端和服务器之间使用实体作为数据传输对象。
您可以通过EventDto(String name, Date start, Date stop, Long deskId)消费新的事件详细信息,然后将其Map到EventEntity

相关问题