zl程序教程

您现在的位置是:首页 >  前端

当前栏目

JSON parse error: Cannot construct instance of “xxx“(although at least one Creator exists)

JSONJSON Error of Cannot at exists One
2023-06-13 09:15:29 时间

今天写SpringBoot出现一个错误

JSON parse error: Cannot construct instance of `priv.kuki.param.AddressListParam` (although at least one Creator exists)

原因

@Data
@NoArgsConstructor // 加上该注解解决问题
public class AddressListParam {
    @NotNull // 加注解报错
    @JsonProperty("user_id")
    private Integer userId;
}

这是一个通过id查询地址的接口,我给id加上不为空的注解后,出现JSON反序列化错误。

解决方案

在类上加上@NoArgsConstructor注解。

错误原因

  • 错误的原因是没有无参构造函数,
  • json在转对象反序列化的时候,需要先初始化对象,默认调用无参构造函数,再赋值,
  • 因而需要创建无参构造器。