具有两个包装器元素的Jackson XML ArrayList输出 [英] Jackson XML ArrayList output having two wrapper elements

查看:278
本文介绍了具有两个包装器元素的Jackson XML ArrayList输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jackson生成的XML输出中得到了两个包装器元素. 我只想要一个.

I am getting two wrapper elements in the XML output generated by Jackson. I would like to have only one.

我有一个Java Bean

I have a Java bean

@Entity
@Table(name = "CITIES")
@JacksonXmlRootElement(localName = "City")
public class City implements Serializable {

    private static final long serialVersionUID = 21L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JacksonXmlProperty(isAttribute = true)
    private Long id;

    @JacksonXmlProperty    
    private String name;

    @JacksonXmlProperty
    private int population;

    // getters, setters etc
}

和一个列表包装器类.

and a List wrapper class.

@JacksonXmlRootElement
public class Cities implements Serializable {

    private static final long serialVersionUID = 22L;

    @JacksonXmlProperty(localName = "City")
    @JacksonXmlElementWrapper(localName = "MyCities")
    private List<City> cities = new ArrayList<>();

    public List<City> getCities() {
        return cities;
    }

    public void setCities(List<City> cities) {
        this.cities = cities;
    }
}

我得到的是此输出,其中包含两个包装器元素.我想要 删除其中之一.

I am getting this output, which has two wrapper elements. I would like to remove one of them.

<Cities>
  <MyCities>
    <City id="1">
      <name>Bratislava</name>
      <population>432000</population>
    </City>
    <City id="2">
      <name>Budapest</name>
      <population>1759000</population>
    </City>
    <City id="3">
      <name>Prague</name>
      <population>1280000</population>
    </City>
  <MyCities>
</Cities>

其中一个来自ArrayList,一个来自该类.如何摆脱包装元素之一?

One of them comes from the ArrayList, one from the class. How to get rid of one of the wrapper elements?

我想要的是这个

<Cities>
    <City id="1">
      <name>Bratislava</name>
      <population>432000</population>
    </City>
    <City id="2">
      <name>Budapest</name>
      <population>1759000</population>
    </City>
    <City id="3">
      <name>Prague</name>
      <population>1280000</population>
    </City>
</Cities>

推荐答案

城市"是根元素,而不是包装器.不会删除实际的包装器元素"MyCities"吗?

"Cities" is the root element, not a wrapper. Wouldn't removing the actual wrapper element "MyCities" work?

添加@JacksonXmlElementWrapper(useWrapping = false)也可能有帮助.

这篇关于具有两个包装器元素的Jackson XML ArrayList输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆