Disons que nous avons une structure JSON qui ressemble à ceci:
{
"field1": "val1",
"field2": "v2",
"f3": "v3",
"f4": "v4",
"arrayOfStuff": [
{
"f5": "v5",
....
"f10": "v10"
}
],
"attributes": [
{"att1": "att1"},
{"att2": "attr2"},
{"att3": "att3"}
],
"options": [
"ignoreMismatchFile"
]
}
Et nos regards de classe Java correspondant comme:
public class Message {
@IsUniqueId
private String field1; //
private String field2;
private String field3;
private String field4;
private List<AnotherObject> f5;
@JsonProperty("attributes")
private LinkedHashMap<String, String> attributes;
private List<String> options;
....
}
Le code d'analyse syntaxique ressemble à:
protected Message loadSavedMessageAsMessageObject(String path) throws IOException {
File file = ResourceUtils.getFile(path);
if (file.exists()) {
ObjectMapper mapper = this.getObjectMapper();
return mapper.readValue(file, Message.class);
}
return null;
}
Nous avons essayé différents moyens d' y parvenir, à l' origine nous avons essayé d'avoir des attributs comme , private List<MessageAttribute> attributes;
mais cela n'a pas fonctionné non plus (nous sommes passés à la carte basée sur une autre réponse - ne fonctionne pas)
Notre objectif est de garder les attributs d'une dynamique, pas la liste codée en dur d'attributs.
Voici comment la MessageAttribute
classe ressemblait à :
public class MessageAttribute {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
L'exception que nous obtenons actuellement est:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap` out of START_OBJECT token
at [Source: (File); line: 32, column: 3] (through reference chain: com.org.Message["attributes"])
Correspondant Message
POJO ci - dessus JSON est en mauvais format, je l' ai fait deux changements attributes
devrait être List of Map
et la liste des AnotherObject
doit pointer versarrayOfStuff
public class Message {
@IsUniqueId
private String field1; //
private String field2;
private String field3;
private String field4;
private List<AnotherObject> arrayOfStuff; //or you can have List<Map<String,String>> arrayOfStuff
@JsonProperty("attributes")
private List<LinkedHashMap<String, String>> attributes; // this is list of map objects
private List<String> options;
....
}