I am trying to deserialize a yaml file using jackson, which works similar to Json, so for convenience i'm going to use Json.
My Json structure looks like this :
{
"version": "2",
"services": {
"app": {
"build": {
"context": "./",
"args": {}
},
"image": "imageName"
},
"results": {
"build": "./",
"image": "imageName"
}
}
}
I want to deserialize this using jackson, but I am getting UnknownPropertyException when using the default ObjectMapper.
This is the class I am trying to deserialize to :
public class ServiceModel {
List<ContainerModel> containers;
public List<ContainerModel> getContainers() {
return containers;
}
public void setContainers(List<ContainerModel> containers) {
this.containers = containers;
}
}
My container model looks something like this :
public class ContainerModel {
@JsonProperty("build")
private String build;
@JsonProperty("args")
private List<String> args;
@JsonProperty("context")
private String context;
@JsonProperty("dockerfile")
private String dockerFile;
// Getters and Setters below
EDIT : I was wrong in describing the Json Structure. Please look at the corrected Json structure that jackson generated when I mapped it to JsonNode
Here, app and results are supposed to be mapped to ContainerModel
Edit
Looking at your new Json structure the ServiceModel class should look like this:
public class ServiceModel {
private String version;
private Map<String, ContainerModel> containers;
// Other fields here...
public void setContainers(Map<String, ContainerModel> containers) {
this.containers = containers;
}
public Map<String, ContainerModel> getContainers() {
return containers;
}
public void setVersion(String version) {
this.version = version;
}
public String getVersion() {
return version;
}
// Other setters/getters here...
}
This way you get your mapping from a key (String), to a ContainerModel.
Original Post
Well, your Json example has a property called services
, and your class has a property called containers
. You shouldn't expect an automatic mapping between those properties.
To map them automatically, use the same property names. If you don't want to, or can't, you can always use Jackson's annotation @JsonProperty
. Additionally, if there are fields that you don't want mapped, you should instantiate a new ObjectMapper, and configure it to ignore unknown fields. Like this:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Also, I am assuming that you get the exception mapping the ServiceModel class, and not the //Known data structure
. But in any case my suggestion should solve the issue in both cases.
It looks like you're attempting to deserialize a property that doesn't exist.
By default, Jackson will throw an exception when you try to deserialize an unknown property. You can disable this by adding the following to your ObjectMapper
instance:
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
You'll likely want to fix the unknown property though. It is likely the services
property, which you've declared as containers
in your Java class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.