0

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"
        }
    ]}
}

1 Answer 1

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;
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

It works but the DataType is not being read and always type = NULL Any Idea? Thanks in advance. @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"), ... }) public abstract static class WidgetBase { @JsonProperty("type") private CustomFieldDataType type; }
@pintekus, added to the answer
If this answer answers your question and was helpful, please accept it as the answer to the question by clicking the check mark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.