我在将JSON反序列化为我的POJO对象时遇到了问题。
这是我的JSON的片段
....."_embedded": {
"media:recent": {
"_links": {
"self": {
"href": "//url"
}
}
}
},....
这是我的班级
@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;
}
}
当我尝试执行mapper.readValue(json,Response.class)时 程序抛出以下错误:
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)
问题可能是属性的名称,因为它有一个冒号。 是否有解决方案来映射这种领域?
由于
答案 0 :(得分:0)
尝试使用root的名称向您的Embedded__类添加@JsonRootName注释:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"media:recent" })
@JsonRootName(value = "_embedded")
public class Embedded__ {
}
将“UNWRAP_ROOT_VALUE”配置添加到您的映射器:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.UNWRAP_ROOT_VALUE, true);
mapper.readValue(json, Response.class)