我有一些jaxb对象建模与具有值的容器对象,可能是另一个容器对象或只是一个简单的对象(例如字符串)的元数据结构。泽西杰克逊重复JSON容器属性
@XmlRootElement(name = "value")
public class Value
{
protected SimpleType type;
protected Container container;
@XmlElement
public SimpleType getType()
{
return type;
}
public void setType(SimpleType type)
{
this.type = type;
}
@XmlInverseReference(mappedBy="value")
@XmlElement
public Container getContainer()
{
return container;
}
public void setContainer(Container container)
{
this.container = container;
}
}
@XmlRootElement(name = "container")
public class Container
{
protected Value value;
@XmlElement
public Value getValue()
{
return value;
}
public void setValue(Value value)
{
this.value = value;
}
}
@XmlRootElement(name = "type")
@XmlEnum
public enum SimpleType
{
@XmlEnumValue("String")STRING,
@XmlEnumValue("Boolean")BOOLEAN,
....etc.
}
XML显示正常,但JSON最终具有重复的“容器”属性。
<container>
<value>
<container>
<value>
<type>String</type>
</value>
</container>
</value>
</container>
"container": {
"value": {
"container": {
"container": {
"value": {
"type": "STRING"
}
}
}
}
}
任何想法为什么这种差异?
有一种解决方法,使JSON看起来像XML? – tanya
已更新的答案。 –
我更新了我的示例w/@XmlInverseReference,但它似乎没有影响json。 – tanya