Spring反序列化失败 Type definition error: [simple type, class xxx.xxx.xxx]

也许更好的阅读体验

Type definition error: [simple type, class com.elm.po.CommonResult]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.elm.po.CommonResult (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

网上搜索得知是反序列化失败了,看到这俩解决办法

  • 需要转换的entity没有getter/setter(本次受此影响,这个不能漏,如果懒得写可以考虑lombok)
  • 没有实现Serializable(本次并不直接对其序列化传输,不涉及)

代码如下

@Data
@AllArgsConstructor
public class CommonResult implements Serializable {
    private Integer code;
    private String message;
    private Object result;
    public static CommonResult success (Object object) {
        return new CommonResult(200, "success", object);
    }
}

可知,实现了Serializable,并且也用了lombok的@Data注解,以上两种方法不能解决问题
问题所在:没有无参构造方法
解决方法:加上@NoArgsConstructor
解决代码

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult implements Serializable {
    private Integer code;
    private String message;
    private Object result;
    public static CommonResult success (Object object) {
        return new CommonResult(200, "success", object);
    }
}

posted @ 2024-03-07 11:57  Morning_Glory  阅读(749)  评论(0编辑  收藏  举报
//