我想创建如下的xml:
<color>black</color>
<size>
<height>1</height>
</size>
我的Pojo课程是:
public class features {
private String color;
@JacksonXmlProperty(localName = "size")
@JsonProperty("value")
private Height height;
//getter, setter, toString
class Height{
@JacksonXmlProperty(localName="height")
public String value;
//getter, setter, toString
}
}
我传递的Json是:
{
颜色:“黑”,
“值”:1
}
我得到的输出是:
<color>black</color>
<size>
<height/>
</size>
为什么不将值设置为高度?
答案 0 :(得分:1)
你的问题是杰克逊只能使用静态内部类。 More info
这样的事情应该有效
static class Height{
@JacksonXmlProperty(localName="height")
public String value;
public Height() {}
public Height(String value) {
this.value = value;
}
}
其他选项是在Features类中使用正确的setter,如
@JsonProperty("value")
public void setHeightFromString(String height){
this.height = new Height();
this.height.value = height;
}
PS:我认为你的json是有效的,看起来像{ "color":"black", "value":"1" }