【发布时间】:2019-04-07 13:35:00
【问题描述】:
我有一个包含两个 LocalDateTime 成员的类:
public class Foo
{
private final LocalDateTime start;
private final LocalDateTime end;
public Foo(LocalDateTime start, LocalDateTime end)
{
this.start = start;
this.end = end;
}
}
我有一个 Spring Boot 控制器来处理 POST 请求:
@PostMapping(value = "/my-resource")
public ResponseEntity<?> bar(@RequestBody Foo foo)
{
return ResponseEntity.ok().build();
}
如果我按如下方式发送 POST 请求:
curl -X POST \
http://localhost:8080/my-resource \
-H 'Content-Type: application/json' \
-d '{
"start":[2016, 1, 1, 10, 24],
"end":[2016, 1, 1, 10, 24]
}
'
返回以下错误:
"message": "Type definition error: [simple type, class Foo]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `Foo` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 2, column: 2]"
经过一番挖掘后,我向 Foo 添加了一个默认 ctor 并解决了错误。我还不得不从成员中删除final。
我不明白为什么这个解决方案有效。有人可以解释一下吗?
【问题讨论】:
标签: java rest spring-boot jackson