实体类序列化报错:Caused by: java.lang.NoSuchMethodException: com.xx.PoJo$Item.<init>()

原实体类代码

@EqualsAndHashCode(callSuper = true)
@Data
public class Pojo extends BaseBean {
    private static final long serialVersionUID = -4291335073882689552L;
    @ApiModelProperty("")
    private Integer id;
    
    ......

    private List list;

    @AllArgsConstructor
    @Data
    public static class Item{
        ......
    }
}

报错信息

org.springframework.beans.InvalidPropertyException: Invalid property 'list[0]' of bean class [com.xx.PoJo]: Illegal attempt to get property 'list' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'list' of bean class [com.xx.PoJo]: Could not instantiate property type [com.xx.PoJo$Item] to auto-grow nested property path; nested exception is java.lang.NoSuchMethodException: com.xx.PoJo$Item.()
	at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:712)
	at org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:843)
	at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:820)
	at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:256)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:104)
	at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:889)
	at org.springframework.validation.DataBinder.doBind(DataBinder.java:780)
	at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:207)
	at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:129)
......
Caused by: org.springframework.beans.NullValueInNestedPathException: Invalid property 'list' of bean class [com.xx.PoJo]: Could not instantiate property type [com.xx.PoJo] to auto-grow nested property path; nested exception is java.lang.NoSuchMethodException: com.xx.PoJo$Item.()
	at org.springframework.beans.AbstractNestablePropertyAccessor.newValue(AbstractNestablePropertyAccessor.java:923)
	at org.springframework.beans.AbstractNestablePropertyAccessor.growCollectionIfNecessary(AbstractNestablePropertyAccessor.java:790)
	at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:659)
	... 57 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.senshun.pay.bus.model.FuiouCloseUserNotify$BankItem.()
	at java.base/java.lang.Class.getConstructor0(Class.java:3349)
	at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
	at org.springframework.beans.AbstractNestablePropertyAccessor.newValue(AbstractNestablePropertyAccessor.java:914)
	... 59 common frames omitted
10:03:40.648 [http-nio-8904-exec-10] WARN  o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved [org.springframework.beans.InvalidPropertyException: Invalid property 'list[0]' of bean class [com.xx.PoJo]: Illegal attempt to get property 'list' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'list' of bean class [com.xx.PoJo]: Could not instantiate property type [com.xx.PoJo$Item] to auto-grow nested property path; nested exception is java.lang.NoSuchMethodException: com.xx.PoJo$Item.()]

原因及解决办法

com.xx.PoJo$Item.()

意思就是实体类Pojo中Item实体类没有无参构造方法。

在Item实体类添加上无参构造方法或者加上@NoArgsConstructor注解即可。

@EqualsAndHashCode(callSuper = true)
@Data
public class Pojo extends BaseBean {
    private static final long serialVersionUID = -4291335073882689552L;
    @ApiModelProperty("")
    private Integer id;
    
    ......

    private List list;

    @NoArgsConstructor    //加上这个注解
    @AllArgsConstructor
    @Data
    public static class Item{
        ......
    }
}

 

你可能感兴趣的:(java,开发语言)