【发布时间】:2019-01-22 22:26:32
【问题描述】:
我正在尝试将 API 的响应映射到我的对象:
class Person {
private Long id;
private String firstname;
private String lastname;
public Person(Long id, String firstname, String lastname)
...
我的 api 调用看起来像:
RestTemplate restTemplate = new RestTemplate();
Person person = restTemplate.getForObject("http://xxx/getPerson", Person.class);
返回的 json 格式如下:
{
"id": 1,
"firstname": "first",
"lastname": "last"
}
不幸的是,我收到以下错误:
Type definition error: [simple type, class xxx.Person]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `xxx.Person` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 4, column: 5]
知道为什么吗? 我的班级中有一个构造函数,所以我不太确定为什么会引发错误。谢谢!
【问题讨论】:
-
在你的 pojo 类中创建默认构造函数
-
既然你有 args 构造函数,你有责任不创建 arg 构造函数
标签: java rest spring-boot resttemplate