Json解析并分配给DTO



解析JSON到DTO时出现问题。

JSON响应为:

{
"content": [
{
"id": 350,
"reg": "FA-2001",
"Email": "abc@gmail.in",
"Mobile": "+9192000000",
"Name": "dr kumar",
"Ip": "0:0:0:0:0:0:0:1",
"Datetime": "2022-10-20T13:50:49",
}
],
"pageable": {
"sort": {
"unsorted": false,
"sorted": true,
"empty": false
},
"pageNumber": 0,
"pageSize": 20,
"offset": 0,
"paged": true,
"unpaged": false
},
"last": true,
"totalPages": 1,
"totalElements": 1,
"first": true,
"sort": {
"unsorted": false,
"sorted": true,
"empty": false
},
"numberOfElements": 1,
"size": 20,
"number": 0,
"empty": false
}

DTO

@Data
public class OtherResponse {
@JsonProperty(value = "content")
private Map content;
}

得到

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.LinkedHashMap<java.lang.Object,java.lang.Object> from Array value (token JsonToken.START_ARRAY)

请建议如何在Map或其他对象中获取值我主要关注的是发送特定的键值对作为响应。

在json字符串中,content是一个列表。您可以做的是相应地更新OtherResponse:

@Data
public class OtherResponse {
@JsonProperty(value = "content")
private List<Map<String, String>> content;
private Pageable pageable;
}

或者甚至为它定义一个专用类:

@Data
public class OtherResponse {
@JsonProperty(value = "content")
private List<Person> content;
private Pageable pageable;
}

PS:更新了答案并添加了Pageable字段。我在这里使用从org.springframework.data.domain.Pageable

导入的Pageable界面

最新更新