Reputation: 457
I have seen and read several questions on this subject, but none seem to address my problem. In all likelihood, I am making one (or two) minor but critical mistakes, but I don't see them.
My JSON looks like this (the real example is much more complex but this is what I have whittled it down to):
[
{
"atcID": "AL011851"
},
{
"atcID": "AL021851"
}
]
The code I used to read it is:
StormData.java:
public class StormData {
@JsonCreator
StormData ( String atcID, String name ) {
this.atcID = atcID;
this.name = name;
};
public String getAtcID() {
return atcID;
}
public void setAtcID( String atcID ) {
this.atcID = atcID;
}
String atcID;
String name;
}
Main file:
byte[] jsonData = Files.readAllBytes(Paths.get(fileName));
ObjectMapper objectMapper = new ObjectMapper();
List<StormData> myObjects = objectMapper.readValue(jsonData , new TypeReference<List<StormData>>(){});
But the error I get is:
Cannot construct instance of `StormData` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
So what am I missing? TIA.
Upvotes: 1
Views: 4022
Reputation: 1167
You need to use both annotations: @JsonCreator
on constructor and @JsonProperty
on each argument:
@JsonCreator
StormData (@JsonProperty("atcID") String atcID, @JsonProperty("name") String name) {
this.atcID = atcID;
this.name = name;
}
See the official documentation.
Since JDK 8 you can also register Jackson ParameterNamesModule
and compile your code with -parameters
option.
See the details in the documentation.
Upvotes: 2