【发布时间】:2018-04-26 00:40:50
【问题描述】:
我正在使用 spring restTemplate 将响应映射到 POJO。 rest api 的响应是这样的:
"attributes": {
"name": {
"type": "String",
"value": ["John Doe"]
},
"permanentResidence": {
"type": "Boolean",
"value": [true]
},
"assignments": {
"type": "Grid",
"value": [{
"id": "AIS002",
"startDate": "12012016",
"endDate": "23112016"
},{
"id": "AIS097",
"startDate": "12042017",
"endDate": "23092017"
}]
}
}
在父类中,我有:
public class Users {
private Map<String, Attribute> attributes;
}
如果所有的值都是字符串类型,那么我可以这样做:
public class Attribute {
private String type;
private String[] value;
}
但值的类型不同。所以我想到了做以下事情:
public class Attribute {
private String type;
private Object[] value;
}
以上应该可以,但在每一步我都必须找出对象的类型。
所以,我的问题是我可以有这样的东西:
public class Attribute {
@JsonProperty("type")
private String type;
@JsonProperty("value")
private String[] stringValues;
@JsonProperty("value")
private Boolean[] booleanValues;
@JsonProperty("value")
private Assignments[] assignmentValues; // for Grid type
}
但它不起作用并抛出错误:Conflicting setter definitions for property "value"
处理这种情况的推荐方法是什么?
【问题讨论】:
标签: java json spring-boot jackson resttemplate