can only instantiate non-static inner class by using default, no-argument constructor

Jackson解析嵌套类问题

问题描述

调用接口返回json格式的数据,使用Jackson解析参数转换成对象:

AjaxResult<PointGoodsResponse> getPointGoodsList(@RequestBody PointGoodsQuery query);

@ApiModel(value = "PointGoodsResponse",description = "积分商品信息")
public class PointGoodsResponse implements Serializable {

    private static final long serialVersionUID = 4790924204646750015L;
    @ApiModelProperty(value = "总条数", dataType = "Integer")
    private Integer totalCount;
    @ApiModelProperty(value = "商品列表", dataType = "list")
    private List<PointGoodsResponse.PointGoods> data = Lists.newArrayList();

    public  class PointGoods {
        @ApiModelProperty(value = "商品ID", dataType = "Long")
        private Long productId;
        @ApiModelProperty(value = "商品名称", dataType = "String")
        private String productName;
        @ApiModelProperty(value = "所需积分", dataType = "Long")
        private Long points;
        @ApiModelProperty(value = "集团id", dataType = "Integer")
        private Integer brandId;
        @ApiModelProperty(value = "酒店id", dataType = "Integer")
        private Integer hotelId;
        @ApiModelProperty(value = "产品价值,单位:分", dataType = "Long")
        private Long price;
        @ApiModelProperty(value = "过期时间", dataType = "String")
        private String expireDate;
        @ApiModelProperty(value = "生效时间", dataType = "String")
        private String effectiveDate;
        @ApiModelProperty(value = "产品数量", dataType = "Integer")
        private Integer count;
        @ApiModelProperty(value = "已兑换数量", dataType = "Integer")
        private Integer exchangedCount;
        @ApiModelProperty(value = "已使用数量", dataType = "Integer")
        private Integer usedCount;
        @ApiModelProperty(value = "产品头图片", dataType = "String")
        private String image;

		//getter、setter方法
		...
    }


    //getter、setter方法
    ...
}

调用该接口,返回错误信息:

"message": "JSON parse error: Can not construct instance of com.zkt.user.api.model.point.PointGoodsResponse$PointGoods: can only instantiate non-static inner class by using default, no-argument constructor; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.zkt.user.api.model.point.PointGoodsResponse$PointGoods: can only instantiate non-static inner class by using default, no-argument constructor at [Source: java.io.PushbackInputStream@edc246; line: 20, column: 9] (through reference chain: com.zkt.user.api.model.point.PointGoodsResponse["data"]->com.zkt.user.api.model.point.PointGoodsResponse$PointGoods["data"]->java.util.ArrayList[0]**)",

解决方法

问题本质为:内部非静态类无法实例化
你需要做两件事:

  • 给内部类前面加上static
  • 给内部类加上默认构造函数

改过后的内部类像这样:

@ApiModel(value = "PointGoodsResponse",description = "积分商品信息")
public class PointGoodsResponse implements Serializable {

    private static final long serialVersionUID = 4790924204646750015L;
    @ApiModelProperty(value = "总条数", dataType = "Integer")
    private Integer totalCount;
    @ApiModelProperty(value = "商品列表", dataType = "list")
    private List<PointGoodsResponse.PointGoods> data = Lists.newArrayList();

    public static class PointGoods {
    	public PointGoods() {
        }
        @ApiModelProperty(value = "商品ID", dataType = "Long")
        private Long productId;
        @ApiModelProperty(value = "商品名称", dataType = "String")
        private String productName;
        @ApiModelProperty(value = "所需积分", dataType = "Long")
        private Long points;
        @ApiModelProperty(value = "集团id", dataType = "Integer")
        private Integer brandId;
        @ApiModelProperty(value = "酒店id", dataType = "Integer")
        private Integer hotelId;
        @ApiModelProperty(value = "产品价值,单位:分", dataType = "Long")
        private Long price;
        @ApiModelProperty(value = "过期时间", dataType = "String")
        private String expireDate;
        @ApiModelProperty(value = "生效时间", dataType = "String")
        private String effectiveDate;
        @ApiModelProperty(value = "产品数量", dataType = "Integer")
        private Integer count;
        @ApiModelProperty(value = "已兑换数量", dataType = "Integer")
        private Integer exchangedCount;
        @ApiModelProperty(value = "已使用数量", dataType = "Integer")
        private Integer usedCount;
        @ApiModelProperty(value = "产品头图片", dataType = "String")
        private String image;

		//getter、setter方法
		...
    }


    //getter、setter方法
    ...
}

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天进步一点_点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值