不能将字符串反序列化为Java对象

huangapple go评论121阅读模式
英文:

Cannot deserialize String to Java object

问题

(数据类)Entity.java

  1. @Data
  2. @Accessors(chain = true)
  3. @AllArgsConstructor
  4. @NoArgsConstructor
  5. public class Entity implements Serializable {
  6. private String id;
  7. private String name;
  8. private String status;
  9. private ZonedDateTime registrationDatetime;
  10. private ZonedDateTime updatedDatetime;
  11. private ZonedDateTime createdDatetime;
  12. }

(数据类)Entities.java

  1. @Data
  2. @AllArgsConstructor
  3. @JsonDeserialize
  4. public class Entities implements Serializable {
  5. private List<Entity> entities;
  6. }

我尝试运行了这个语句:

  1. TypeReference<ResponseModel<Entities>> typeReference = new TypeReference<ResponseModel<Entities>>() {};
  2. ResponseModel<Entities> response = objectMapper.readValue(result.getResponse().getContentAsString(), typeReference);

然后我得到了这个错误:

  1. 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

  1. @Data
  2. @Accessors(chain = true)
  3. @AllArgsConstructor
  4. @NoArgsConstructor
  5. public class Entity implements Serializable {
  6. private String id;
  7. private String name;
  8. private String status;
  9. private ZonedDateTime registrationDatetime;
  10. private ZonedDateTime updatedDatetime;
  11. private ZonedDateTime createdDatetime;
  12. }

(Data class) Entities.java

  1. @Data
  2. @AllArgsConstructor
  3. @JsonDeserialize
  4. public class Entities implements Serializable {
  5. private List&lt;Entity&gt; entities;
  6. }

I tried to run this statement:

  1. TypeReference&lt;ResponseModel&lt;Entities&gt;&gt; typeReference = new TypeReference&lt;ResponseModel&lt;Entities&gt;&gt;() {};
  2. ResponseModel&lt;Entities&gt; response = objectMapper.readValue(result.getResponse().getContentAsString(), typeReference);

And I got this error:

  1. 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.

huangapple
  • 本文由 发表于 2020年4月8日 21:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61101462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定