简体   繁体   English

杰克逊将未知属性名称反序列化为自定义实体

[英]jackson deserialize unknown property name to custom entity

I am trying to deserialize a yaml file using jackson, which works similar to Json, so for convenience i'm going to use Json. 我正在尝试使用jackson反序列化yaml文件,该文件的工作方式类似于Json,因此为了方便起见,我将使用Json。

My Json structure looks like this : 我的Json结构如下所示:

 {
    "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. 我想使用杰克逊反序列化此方法,但是使用默认的ObjectMapper时却出现UnknownPropertyException。

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. 编辑:我在描述Json结构时是错误的。 Please look at the corrected Json structure that jackson generated when I mapped it to JsonNode 请查看杰克逊将其映射到JsonNode时生成的更正的Json结构

Here, app and results are supposed to be mapped to ContainerModel 在这里,应将应用程序和结果映射到ContainerModel

Edit 编辑

Looking at your new Json structure the ServiceModel class should look like this: 查看新的Json结构,ServiceModel类应如下所示:

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. 这样,您就可以从键(字符串)到ContainerModel的映射。

Original Post 原始帖子

Well, your Json example has a property called services , and your class has a property called containers . 好吧,您的Json示例具有一个称为services的属性,而您的类具有一个名为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 . 如果您不想或不能,可以随时使用Jackson的批注@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. 此外,如果存在您不想映射的字段,则应实例化一个新的ObjectMapper,并将其配置为忽略未知字段。 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 . 另外,我假设您获得映射ServiceModel类的异常,而不是//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. 默认情况下,当您尝试反序列化未知属性时,Jackson将引发异常。 You can disable this by adding the following to your ObjectMapper instance: 您可以通过将以下内容添加到ObjectMapper实例来禁用此功能:

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. 可能是services属性,您已在Java类中将其声明为containers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM