admin管理员组

文章数量:1391860

Tried to use Spring Boot to load the different DataType

    PLAIN_TEXT_INPUT("plain_text_input"),
    RADIO_BUTTONS("radio_buttons"),
    CHECKBOXES("checkboxes"),
    SELECT("select")

But always getting the exception of not marked as ignorable (3 known properties: "actionId", "type", "text"]) at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION disabled); line: 12, column: 37] (through reference chain: configuration$Body["prefix"]->Prefix["aLevel"]->java.util.ArrayList[0]->xxx.$WidgetBase["placeholder"])

Can anyone help to fix this issue?

@Data
public static class Prefix {
    @JsonProperty("aLevel")
    private List<WidgetBase> aLevel;
}

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = TextInput.class, name = "plain_text_input"),
    @JsonSubTypes.Type(value = RadioGroup.class, name = "radio_buttons"),
    @JsonSubTypes.Type(value = Select.class, name = "select"),
    @JsonSubTypes.Type(value = CheckBoxGroup.class, name = "checkboxes")
})
public static class WidgetBase  {
    @JsonProperty("actionId")
    private String actionId;
    @JsonProperty("type")
    private DataType type;
    @JsonProperty("text")
    private String text;
}
@Data
public static class TextInput extends WidgetBase {
    @JsonProperty("placeholder")
    private String placeholder;
    @JsonProperty("description")
    private String description;
}

@Data
public static class CheckBoxGroup extends WidgetBase {
    @JsonProperty("options")
    private List<CheckBoxOption> option_items;
}

@Data
public static class Select extends WidgetBase {
    @JsonProperty("selected_item")
    private Option selected_item;
    @JsonProperty("select_items")
    private List<Option> select_items;
}

@Data
public static class Option  {
    @JsonProperty("text")
    private String text;
    @JsonProperty("value")
    private String value;
}

and the payload can be as follow

"prefix":{
    "aLevel":[
    {
        "actionId":"plain_text_input_3",
        "type":"plain_text_input",
        "text":"Account ID",
        "placeholder":"Input your Account ID",
        "description":"Please enter the Account ID"
    },
    {
        "actionId":"radio_group_action_3",
        "type":"radio_buttons",
        "text":"Radio Select",
        "initial_option":{
            "text":"Radio 1",
            "value":"value1"
        },
        "option_items":[
        {
            "text":"Option 1",
            "value":"value1"
        },
        {
            "text":"Option 2",
            "value":"value2"
        },
        {
            "text":"Option 3",
            "value":"value3"
        }]
    },
    {
        "actionId":"checkbox_action_3",
        "text":"Checkbox Group",
        "type":"checkboxes",
        "options":[
        {
            "text":"Option 1",
            "value":"value1",
            "initial_selected":true
        }]
    },
    {
        "actionId":"select_action_3",
        "type":"select",
        "text":"This is static select label",
        "selected_item":{
            "text":"Option 1",
            "value":"value1"
        },
        "select_items":[
        {
            "text":"Option 1",
            "value":"value1"
        },
        {
            "text":"Option 2",
            "value":"value2"
        },
        {
            "text":"Option 3",
            "value":"value3"
        }
    ]}
}

Tried to use Spring Boot to load the different DataType

    PLAIN_TEXT_INPUT("plain_text_input"),
    RADIO_BUTTONS("radio_buttons"),
    CHECKBOXES("checkboxes"),
    SELECT("select")

But always getting the exception of not marked as ignorable (3 known properties: "actionId", "type", "text"]) at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION disabled); line: 12, column: 37] (through reference chain: configuration$Body["prefix"]->Prefix["aLevel"]->java.util.ArrayList[0]->xxx.$WidgetBase["placeholder"])

Can anyone help to fix this issue?

@Data
public static class Prefix {
    @JsonProperty("aLevel")
    private List<WidgetBase> aLevel;
}

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = TextInput.class, name = "plain_text_input"),
    @JsonSubTypes.Type(value = RadioGroup.class, name = "radio_buttons"),
    @JsonSubTypes.Type(value = Select.class, name = "select"),
    @JsonSubTypes.Type(value = CheckBoxGroup.class, name = "checkboxes")
})
public static class WidgetBase  {
    @JsonProperty("actionId")
    private String actionId;
    @JsonProperty("type")
    private DataType type;
    @JsonProperty("text")
    private String text;
}
@Data
public static class TextInput extends WidgetBase {
    @JsonProperty("placeholder")
    private String placeholder;
    @JsonProperty("description")
    private String description;
}

@Data
public static class CheckBoxGroup extends WidgetBase {
    @JsonProperty("options")
    private List<CheckBoxOption> option_items;
}

@Data
public static class Select extends WidgetBase {
    @JsonProperty("selected_item")
    private Option selected_item;
    @JsonProperty("select_items")
    private List<Option> select_items;
}

@Data
public static class Option  {
    @JsonProperty("text")
    private String text;
    @JsonProperty("value")
    private String value;
}

and the payload can be as follow

"prefix":{
    "aLevel":[
    {
        "actionId":"plain_text_input_3",
        "type":"plain_text_input",
        "text":"Account ID",
        "placeholder":"Input your Account ID",
        "description":"Please enter the Account ID"
    },
    {
        "actionId":"radio_group_action_3",
        "type":"radio_buttons",
        "text":"Radio Select",
        "initial_option":{
            "text":"Radio 1",
            "value":"value1"
        },
        "option_items":[
        {
            "text":"Option 1",
            "value":"value1"
        },
        {
            "text":"Option 2",
            "value":"value2"
        },
        {
            "text":"Option 3",
            "value":"value3"
        }]
    },
    {
        "actionId":"checkbox_action_3",
        "text":"Checkbox Group",
        "type":"checkboxes",
        "options":[
        {
            "text":"Option 1",
            "value":"value1",
            "initial_selected":true
        }]
    },
    {
        "actionId":"select_action_3",
        "type":"select",
        "text":"This is static select label",
        "selected_item":{
            "text":"Option 1",
            "value":"value1"
        },
        "select_items":[
        {
            "text":"Option 1",
            "value":"value1"
        },
        {
            "text":"Option 2",
            "value":"value2"
        },
        {
            "text":"Option 3",
            "value":"value3"
        }
    ]}
}
Share Improve this question edited Mar 13 at 5:44 Geii Lvov 3,1151 gold badge8 silver badges29 bronze badges asked Mar 12 at 2:53 pintekuspintekus 252 silver badges7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Currently, you are trying to deserialize the placeholder property into the TextInput, without telling Jackson that it is TextInput. Jackson treats it as a simple WidgetBase, therefore UnrecognizedPropertyException occurs.

You have to properly configure inheritance in Jackson to deserialize WidgetBase and its subclasses such as TextInput, CheckBoxGroup, etc.

Consider this approach that uses @JsonTypeInfo and @JsonSubTypes annotations to set up inheritance.

Besides, I don't see a value for TextInput in the DataType enum. Additionally, there are some mistakes in the json file, such as the option_items property, even though you have configured @JsonProperty("options"). Please double-check your json file and classes for such small errors.

To deserialize DataType by value add @JsonValue annotation to its getter:

public enum DataType {
        RADIO_BUTTONS("radio_buttons"),
        CHECKBOXES("checkboxes"),
        SELECT("select");

        private final String value;

        DataType(String value) {
            this.value = value;
        }

        @JsonValue
        public String getValue() {
            return value;
        }
    }

本文标签: javaspring boot reading dynamic data with inheritanceStack Overflow