【问题标题】:Java lombok universal field for list of objects用于对象列表的 Java lombok 通用字段
【发布时间】:2022-10-04 16:08:47
【问题描述】:

我有来自 api 的 json 响应,其结构如下:

{
    "countPerPage": 20,
    "totalCount": 401,
    "currentPage": 1,
    "totalPage": 21,
    "data": [
        {
            "id": 1,
            "catId": 12,
            "dogId": 12,
            "creationDate": "2022-01-03 12:29:38",
            "comment": "Some comment"
        },
        {
            "id": 2,
            "catId": 13,
            "dogId": 16,
            "creationDate": "2022-01-08 11:14:25",
            "comment": "Some comment"
        },
        ...
    ]
}

像这样

{
    "countPerPage": 20,
    "totalCount": 226,
    "currentPage": 3,
    "totalPage": 12,
    "data": [
        {
            "id": 1,
            "parentId": 12,
            "firstName": "John",
            "lastName": "Doe",
            "creationDate": "2022-01-03 12:29:38",
            "age": 25
        },
        {
            "id": 1,
            "parentId": 12,
            "firstName": "Michael",
            "lastName": "Finder",
            "creationDate": "2022-01-08 11:14:25",
            "age": 24
        },
        ...
    ]
}

其他结构相同。

如果我为这样的人创建响应 java 类

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PersonResponse{
    @JsonProperty("id")
    private int id;

    @JsonProperty("parentId")
    private int parentId;

    @JsonProperty("firstName")
    private String firstName;

    @JsonProperty("lastName")
    private String lastName;

    @JsonProperty("creationDate")
    private String creationDate;

    @JsonProperty("age")
    private int age;
}

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PersonWithCount {
    @JsonProperty("countPerPage")
    private int countPerPage;

    @JsonProperty("totalCount")
    private int totalCount;

    @JsonProperty("currentPage")
    private int currentPage;

    @JsonProperty("totalPage")
    private int totalPage;

    @JsonProperty("data")
    private List<PersonResponse> data;
}

对于这样的动物

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class AnimalResponse{
    @JsonProperty("id")
    private int id;

    @JsonProperty("catId")
    private int catId;

    @JsonProperty("dogId")
    private int dogId;

    @JsonProperty("creationDate")
    private String creationDate;

    @JsonProperty("comment")
    private String comment;
}

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class AnimalWithCount {
    @JsonProperty("countPerPage")
    private int countPerPage;

    @JsonProperty("totalCount")
    private int totalCount;

    @JsonProperty("currentPage")
    private int currentPage;

   @JsonProperty("totalPage")
    private int totalPage;

    @JsonProperty("data")
    private List<AnimalResponse> data;
}

所有工作正确。 但是我怎样才能使用通用的东西,比如泛型,因为不要复制和粘贴每个时间类的相同数据,只更改最后一个字段?

我期待这样的事情,但它不工作

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataWithCount<T> {
    @JsonProperty("countPerPage")
    private int countPerPage;

    @JsonProperty("totalCount")
    private int totalCount;

    @JsonProperty("currentPage")
    private int currentPage;

    @JsonProperty("totalPage")
    private int totalPage;

    @JsonProperty("data")
    private List<T> data;
}

填充变量:

personListWithCount = ObjectMapperCreator.objectMapperCreator().readValue(personResponse.getBody().asPrettyString(), PersonListWithCount.class);

也许我做错了什么?

【问题讨论】:

  • 这是正确的解决方案,你得到了什么?
  • 我不能设置泛型类型,因为 PersonListWithCount.class 不能像 PersonListWithCount<AnimalResponse>.class 作为参数
  • 这回答了你的问题了吗? Jackson - Deserialize using generic class
  • @viking 使用参数 ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT、ACCEPT_SINGLE_VALUE_AS_ARRAY、FAIL_ON_UNKNOWN_PROPERTIES 创建并重新调整 ObjectMapper

标签: java lombok


【解决方案1】:

帮助回答其他问题(Jackson - Deserialize using generic class):

mapper.readValue(jsonString, new TypeReference<Data<String>>() {});

谢谢@尼克

【讨论】:

    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2018-10-03
    • 2017-07-25
    • 2017-03-06
    • 2020-07-17
    • 1970-01-01
    相关资源
    最近更新 更多