Cannot serialize; nested exception is nested exception is java.io.NotSerializableException:

在將實體類以Object類型存儲到redis中報了未序列化異常。。

Cannot serialize; nested exception is
org.springframework.core.serializer.support.SerializationFailedException:Failed
to serialize object using DefaultSerializer; nested exception is
java.io.NotSerializableException: xxx
@Data
@TableName("t_charger_pile")
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ChargerEntity implements Serializable {
	private static final long serialVersionUID = 42L;
	...
}

發現其實這個實體類是有實現序列化的。。後來想想,,錯誤提示一般不會出錯。。那就還有一種可能。。它內部的實體類可能未實現序列化。仔細檢查代碼:

@Data
@TableName("t_charger_pile")
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ChargerEntity implements Serializable {
	private static final long serialVersionUID = 42L;
	...
	
	@TableField(exist = false)
	@NotEmpty(message = "槍信息不能爲空")
	private List<ChargerGunEntity> list;
}

檢查 ChargerGunEntity 實體類:


@Data
@TableName("t_charger_pile_gun")
public class ChargerGunEntity  {
	private Long id;
	....
}

果然發現未實現序列化,修改代碼如下:

@Data
@TableName("t_charger_pile_gun")
public class ChargerGunEntity implements Serializable {
	private static final long serialVersionUID = 42L;
	...
}

重新啓動項目,重新執行,,問題解決!
總結:redis在存儲對象時,對象及對象中的所有子對象必須都實現序列化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章