1

I'm having problems deserializing JSON into my POJO object.

This is a snippet of my JSON

....."_embedded": {
"media:recent": {
    "_links": {
        "self": {
            "href": "//url"
        }
    }
}

},....

And here is my Class

    @JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "media:recent"
})
public class Embedded__ {

    @JsonProperty("media:recent")
    private MediaRecent mediaRecent;

    /**
     * 
     * @return
     *     The mediaRecent
     */
    @JsonGetter("media:recent")
    public MediaRecent getMediaRecent() {
        return mediaRecent;
    }

    /**
     * 
     * @param mediaRecent
     *     The media:recent
     */
    @JsonSetter("media:recent")
    public void setMediaRecent(MediaRecent mediaRecent) {
        this.mediaRecent = mediaRecent;
    }

}

When I try to execute mapper.readValue(json, Response.class) the program is throwing the following error:

    Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "media:recent" (Class ...Embedded__), not marked as ignorable
 at [Source: java.io.StringReader@1fe02e4; line: 69, column: 22] (through reference chain: ......Response["data"]->......Data["_embedded"]->......Embedded["media"]->......Medium["_embedded"]->......Embedded_["uploader"]->......Uploader["_embedded"]->......Embedded__["media:recent"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)

Probably, the problem is the name of the property because it has a colon. Is there a solution in order to map this kind of field?

Thanks

1 Answers1

0

Try to add to your Embedded__ class a @JsonRootName annotation with the name of the root:

    @JsonInclude(JsonInclude.Include.NON_NULL) 
    @Generated("org.jsonschema2pojo") 
    @JsonPropertyOrder({
    "media:recent" }) 
    @JsonRootName(value = "_embedded") 
    public class Embedded__ {
    }

And add the "UNWRAP_ROOT_VALUE" config to your mapper:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.UNWRAP_ROOT_VALUE, true);
mapper.readValue(json, Response.class) 
poozmak
  • 414
  • 2
  • 11