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方法
    ...
}

你可能感兴趣的:(Java)