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

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

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&lt;Entity&gt; entities;
}

I tried to run this statement:

TypeReference&lt;ResponseModel&lt;Entities&gt;&gt; typeReference = new TypeReference&lt;ResponseModel&lt;Entities&gt;&gt;() {};
ResponseModel&lt;Entities&gt; 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.

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:

确定