Jackson XML系列化不包装子级,而使用子级元素名称

时间:2018-09-04 08:55:33

标签: jackson

我要在序列化如下所示的类结构时生成以下XML:

<C>
  <D A="FrontRight"/>
  <W A="Green"/>
</C>

Java代码如下:

@JacksonXmlRootElement(localName = "C")
public static class Car {
    public Car(List children) {
        this.children = children;
    }

    @JacksonXmlElementWrapper(useWrapping = false)
    public List children;
}

@JacksonXmlRootElement(localName = "D")
public static class Door {

    @JacksonXmlProperty(localName = "A", isAttribute = true)
    public String placement = "FrontRight";
}

@JacksonXmlRootElement(localName = "W")
public static class Window {
    @JacksonXmlProperty(localName = "A", isAttribute = true)
    public String color = "Green";
}


    @JacksonXmlElementWrapper(useWrapping = false)
    public List children;
}

然后像这样调用映射器:

    ObjectMapper mapper = new XmlMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    System.out.println(mapper.writeValueAsString(new Car(Arrays.asList(new Door(), new Window()))));

产生此输出:

<C>
  <children A="FrontRight"/>
  <children A="Green"/>
</C>

我应该怎么做才能摆脱children,并让它使用列表中列出的类上的带注释的名称?

0 个答案:

没有答案