Deserializing enum Shape.OBJECT using Jackson not working

I have the following listing declaration:

@Document
@JsonFormat(shape= JsonFormat.Shape.OBJECT)
@JsonAutoDetect()
public enum Compass {
    north("Upper Center"),
    south("Lower Center"),
    east("Left Center"),
    west("Right Center"),
    ne("Upper Right"),
    nw("Upper Left"),
    se("Lower Right"),
    sw("Lower Left"),
    ;

    @JsonProperty   
    private String presentableName;
    @JsonProperty   
    private String name;

    private Compass() {}
    private Compass(String presentableName) {
        this.presentableName = presentableName;
    }

    public String getPresentableName() {
        return presentableName;
    }
    public void setPresentableName(String presentableName) {
        this.presentableName = presentableName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @JsonCreator
    public static Compass fromObject(@JsonProperty("name") String name, @JsonProperty("presentableName") String presentableName) { 
        return Compass.sw;
    }
}

The input comes as a json object, and most of it is deserialized correctly, but the corresponding part has the following form, where placement- Compass:

{"placement":{"name":"se","presentableName":"Lower Right"}}

Decontamination does not work. I thought it JsonCreatorwould work here, but for some reason I get

org.springframework.web.HttpMediaTypeNotSupportedException: content type 'application / json; charset = UTF-8 'is not supported

which is really just a symptom of deserialization failure.

If I changed the creator to:

@JsonCreator
public static Compass fromObject(@JsonProperty("name") String name) { 
    return Compass.sw;
}

, { se ( json, , , , , )

jackson 2.2.3, .

+4
2

@JsonFormat, , Enums , , JsonFormat. Shape.Object. , Enums, . , , .

+3

- @Renatinn, (- ) .

@JsonCreator, Map<String, Object>. , :

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum TesterEnum {
    FirstValue(1, "One"),
    SecondValue(2, "Two");

    private int id;
    private String dsc;

    TesterEnum(int id, String dsc) {
        this.id = id;
        this.dsc = dsc;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getDsc(){
        return dsc;
    }
    public void setDsc(String dsc){
        this.dsc = dsc;
    }

    @JsonCreator
    public static TesterEnum fromObject(final Map<String, Object> obj) {
        if (obj != null && obj.containsKey("id")) {
            Integer id = null;
            if (obj.get("id") instanceof Integer) {
                id = (Integer)obj.get("id");
            } else {
                id = Integer.parseInt((String)obj.get("id"));
            }
            return fromId(id);
        }
        return null;
    }
    public static TesterEnum fromId(final Integer id) {
        if (id != null) {
            for (TesterEnum e : TesterEnum.values()) {
                if (id.equals(e.getId())) return e;
            }
        }
        return null;
    }
}

P.S.: fromId, fromObject, .

PS²: id Integer, if (obj.get("id") instanceof Integer) fromObject, , String, .

+1

Source: https://habr.com/ru/post/1542223/


All Articles