简体   繁体   English

Jackson XML 序列化:与父级同名的嵌套元素

[英]Jackson XML Serialization: Nested Element with Same Name as Parent

I have a complex xml type with a nested element whose name is the same as the parent, but when I serialize it the nested element gets set as the text of the parent when it is included in another element.我有一个复杂的 xml 类型,其中包含一个名称与父元素相同的嵌套元素,但是当我对其进行序列化时,嵌套元素在包含在另一个元素中时被设置为父元素的文本。

The DTOs looks like this: DTO 看起来像这样:

@JacksonXmlRootElement(localName = "address")
public class Address{
    @JacksonXmlText(false)
    @JsonProperty
    private String address;
    @JsonProperty
    private String city;
    //getters and setters
}

@JacksonXmlRootElement(localName = "person")
public Person {
    private String name;
    private Address address;
}

My XmlMapper configuration:我的 XmlMapper 配置:

XmlMapper xmlMapper = XmlMapper.xmlBuilder()
    .defaultUseWrapper(false)
    .serializationInclusion(NON_NULL)
    .build();
xmlMapper.getFactory()
    .getXMLOutputFactory()
    .setProperty("javax.xml.stream.isRepairingNamespaces", false);

When I serialize an Address on it's own, I get the expected value:当我自己序列化一个地址时,我得到了预期值:

<address><address>123 East Street</address><city>metropolis</city></address>

But when I serialize an Address as a nested object of another DTO, like Person, then the address property is serialized as text of the Address parent object.但是当我将 Address 序列化为另一个 DTO 的嵌套对象时,例如 Person,则 address 属性将序列化为 Address 父对象的文本。

Actual XML:实际的 XML:

<person><address>123 East Street<city>metropolis</city></address></person>

Expected XML:预期的 XML:

<person><address><address>123 East Street</address><city>metropolis</city></address></person>

I already know that this is just bad XML design, but that is what I have to do!我已经知道这只是糟糕的XML 设计,但这就是我必须做的!

Any ideas on how to get the expected XML output?关于如何获得预期的 XML 输出的任何想法?

Edits编辑

I found a workaround by creating another DTO with a "value" field marked as text.我通过创建另一个带有标记为文本的“值”字段的 DTO 找到了一种解决方法。

@JacksonXmlRootElement(localName = "address")
public class StreetAddress{
    @JacksonXmlText
    private String value;
    //getters and setters
}
//snip
@JacksonXmlRootElement(localName = "address")
public class Address{
    @JsonProperty
    private StreetAddress address;
    @JsonProperty
    private String city;
    //getters and setters
}

When wiring the DTOs like follows, the XML output is produced like expected.当按如下方式连接 DTO 时,XML 输出将按预期生成。 I used version 2.10 of jackson-annotations and jackson-dataformat-xml .我使用了jackson-annotationsjackson-dataformat-xml 2.10版。 No differences to the code except additional getters and setters.除了额外的 getter 和 setter 之外,与代码没有区别。

Address a = new Address();
a.setCity("metropolis");
a.setAddress("123 East Street");
Person p = new Person();
p.setAddress(a);
xmlMapper.writeValue(new PrintWriter(System.out), p);

Output输出

<person><address><address>123 East Street</address><city>metropolis</city></address></person>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM