1. ホーム
  2. Java

静的でない内部クラスをインスタンス化するには、デフォルトの無引数コンストラクタを使用する必要があります。

2022-02-22 12:57:15
<パス

Jackson はネストされたクラスの問題を解析する

質問内容

json形式のデータを返すインターフェースを呼び出し、Jacksonを使ってパラメータをオブジェクトにパースする。

AjaxResult
 getPointGoodsList(@RequestBody PointGoodsQuery query);


@ApiModel(value = "PointGoodsResponse",description = "PointGoodsInfo")
public class PointGoodsResponse implements Serializable {

    private static final long serialVersionUID = 4790924204646750015L;
    @ApiModelProperty(value = "TotalCount", dataType = "Integer")
    private Integer totalCount;
    @ApiModelProperty(value = "Product list", dataType = "list")
    private List
Calling this interface returns the error message.
"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]**)",


Solution
The nature of the problem is.
Internal non-static classes cannot be instantiated

You need to do two things. Precede the inner class with static Add a default constructor to the inner class The altered inner class looks like this. @ApiModel(value = "PointGoodsResponse",description = "PointGoodsInfo") public class PointGoodsResponse implements Serializable { private static final long serialVersionUID = 4790924204646750015L; @ApiModelProperty(value = "TotalCount", dataType = "Integer") private Integer totalCount; @ApiModelProperty(value = "Product list", dataType = "list") private List
"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]**)",