Java:对象进行对象的列表
我有这个json :(作为map< string,object>
)
{
"id": 1000,
"lab": [
"LAB1",
"LAB2",
"LAB3"
],
"name": "TEST",
"ref": {
"id": 1000,
"code": "REFCODE",
"description": "REF DESC"
},
"employee": {
"id": 1000,
"name": "Emp1000",
"tin": null,
"active": true
},
"contacts": [
{
"id": 1000,
"name": "Contact 1",
"emailAddress": "[email protected]",
"active": true,
"positions": [
{
"position": {
"id": 1000,
"code": "POS",
"description": "POS DESC"
}
}
]
}
],
"status": "NEW"
}
这是我的DTO和ContactDto:
public class DTO {
private Long id;
...
@JsonProperty("contacts")
private List<ContactDTO> contacts;
}
@Builder
public class ContactDTO implements Serializable {
private Long id;
private String name;
private String emailAddress;
private Boolean active;
@NotEmpty
private List<ContactPositionDTO> positions;
}
这是我的服务类,带有对象映射器和过程方法,该方法接受JSON MAP:
private ObjectMapper objectMapper() {
var objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
return objectMapper;
}
public void process(final Map<String, Object> map) {
objectMapper().convertValue(map, DTO.class);
}
但是,我是获取java.lang.lang.illegalargumentException:无法应对类型的java.util.arraylist
的值
,如果我添加deserializationfeature.accept_single_value_value_as_ars_array 我得到了不同的错误:
java。 lang.illegalgumentException:无法构造contactdto的实例(尽管至少存在一个创建者):没有字符串argument constructor/factory方法可以从字符串值中进行估算('[{iD = 1000,name = contact 1 ,, 1 ,, ..... << /代码>
I have this JSON: (passed as Map<String, Object>
)
{
"id": 1000,
"lab": [
"LAB1",
"LAB2",
"LAB3"
],
"name": "TEST",
"ref": {
"id": 1000,
"code": "REFCODE",
"description": "REF DESC"
},
"employee": {
"id": 1000,
"name": "Emp1000",
"tin": null,
"active": true
},
"contacts": [
{
"id": 1000,
"name": "Contact 1",
"emailAddress": "[email protected]",
"active": true,
"positions": [
{
"position": {
"id": 1000,
"code": "POS",
"description": "POS DESC"
}
}
]
}
],
"status": "NEW"
}
This is my DTO and ContactDTO:
public class DTO {
private Long id;
...
@JsonProperty("contacts")
private List<ContactDTO> contacts;
}
@Builder
public class ContactDTO implements Serializable {
private Long id;
private String name;
private String emailAddress;
private Boolean active;
@NotEmpty
private List<ContactPositionDTO> positions;
}
Here is my service class with object mapper and process method which accepts the JSON map:
private ObjectMapper objectMapper() {
var objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
return objectMapper;
}
public void process(final Map<String, Object> map) {
objectMapper().convertValue(map, DTO.class);
}
However, I am getting java.lang.IllegalArgumentException: Cannot deserialize value of type java.util.ArrayList
And if I add DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY
I am getting a different error:
java.lang.IllegalArgumentException: Cannot construct instance of ContactDTO (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[{id=1000, name=Contact 1, .....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两个替代选项来修复
contactdto
类:公共contactdto(){}
上课。要修复当时即将发生的编译器错误
您将需要删除
@builder
注释。@builder
注释并将
@jacksonized
注释到类。这将配置生成的构建器以合作
杰克逊的挑战。
有关更多详细信息,请参见 lombok关于
@jacksonized
的文档。You have two alternative options to fix your
ContactDTO
class:public ContactDTO() { }
to the class. To fix the then upcoming compiler error
you will need to remove the
@Builder
annotation.@Builder
annotationand add the
@Jacksonized
annotation to the class.This will configure the generated builder to cooperate
with Jackson's deserialization.
For more details see Lombok's documentation about
@Jacksonized
.为了进行挑选,您需要一个no arg构造函数并使用
@builder
您需要一个All arg构造函数。因此,您需要添加两者。
下面的示例应起作用。我刚刚添加了
@getter
注释以避免使用@jsonproperty
nb:您还可以使用
@jacksonized
注释,而不是@noargsconsstructorr 和
@allargsconstructor
To deserialize you need a No arg constructor and to use
@Builder
you need an all arg constructor.So you need tu add both.
The example below should work. I just added
@Getter
annotation to avoid using@JsonProperty
NB: you can also use
@Jacksonized
annotation instead of@NoArgsConstructor
and@AllArgsConstructor