英文:
Cannot deserialize String to Java object
问题
(数据类)Entity.java
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class Entity implements Serializable {
private String id;
private String name;
private String status;
private ZonedDateTime registrationDatetime;
private ZonedDateTime updatedDatetime;
private ZonedDateTime createdDatetime;
}
(数据类)Entities.java
@Data
@AllArgsConstructor
@JsonDeserialize
public class Entities implements Serializable {
private List<Entity> entities;
}
我尝试运行了这个语句:
TypeReference<ResponseModel<Entities>> typeReference = new TypeReference<ResponseModel<Entities>>() {};
ResponseModel<Entities> response = objectMapper.readValue(result.getResponse().getContentAsString(), typeReference);
然后我得到了这个错误:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of 'model.Entities' (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
我已经添加了@JsonDeserialize
,还有implements Serializable
,我漏掉了什么?
英文:
(Data class) Entity.java
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class Entity implements Serializable {
private String id;
private String name;
private String status;
private ZonedDateTime registrationDatetime;
private ZonedDateTime updatedDatetime;
private ZonedDateTime createdDatetime;
}
(Data class) Entities.java
@Data
@AllArgsConstructor
@JsonDeserialize
public class Entities implements Serializable {
private List<Entity> entities;
}
I tried to run this statement:
TypeReference<ResponseModel<Entities>> typeReference = new TypeReference<ResponseModel<Entities>>() {};
ResponseModel<Entities> response = objectMapper.readValue(result.getResponse().getContentAsString(), typeReference);
And I got this error:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `model/Entities` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
I already put @JsonDeserialize
and also implements Serializable
what did I miss?
答案1
得分: 1
尝试为实体类也提供 @NoArgsConstructor。这应该有效。
英文:
Try giving @NoArgsConstructor also for Entities class. This should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论