简体   繁体   English

JAXB,公共字段映射行为与带有@XmlElement批注的私有字段不同

[英]JAXB, public fields mapping behavior differs from private fields with @XmlElement annotation

Should @XmlElement annotation force the mapping of a field as it was a public field? @XmlElement注释是否应该强制映射字段,因为它是公共字段?

That is, the field: 也就是说,该字段:

public HashMap<String, String> myMap = new HashMap<String, String>();

get mapped ok. 确定映射。
Regardless the things that are said around the web about mapping Map s, when i Marshall my class this produces a nice: 无论在网络上有关映射Map的内容如何,​​当我在Marshall上我的课程时,都会产生一个不错的效果:

<myMap>
    <entry>
        <key>cat</key> <value>meows</value>
    </entry>
    <entry>
        <key>dog</key> <value>barks</value>
    </entry>
</myMap>

but if i add a second identical field, but for the fact that is private and so, in order to be mapped, annotated with @XmlElement : 但是,如果我添加第二个相同的字段,但由于是私有的事实,为了进行映射,请使用@XmlElement注释:

@XmlElement
private HashMap<String, String> myMap2 = new HashMap<String, String>();

it doesn't work any more, that is it produces an empty element: 它不再起作用,那就是产生一个空元素:

<myMap2/>

So @XmlElement doesn't just "turn on" mapping . 因此,@ XmlElement不仅可以“打开”映射 Well, it does for simple type fields, even for Lists but NOT for Maps. 好吧,它适用于简单的类型字段,甚至适用于列表,但不适用于地图。

The question is: is it a wrong (naive) interpretation of the @XmlElement? 问题是:@XmlElement的解释是错误的(天真)吗? Is there a way to accomplish just what JAXB already does for public Map fields without developing adapters? 有没有一种方法可以完成JAXB已经为公共Map字段完成的工作而无需开发适配器?

This should be enough, just in case I've pasted the complete, yet minimal, source code here: 以防万一我在此处粘贴了完整但最少的源代码:

@XmlRootElement
public class K {

    public HashMap<String, String> myMap = new HashMap<String, String>();

    @XmlElement
    private HashMap<String, String> myMap2 = new HashMap<String, String>();

    //An Array list that yet gets correctly mapped
    @XmlElement
    private ArrayList<String> myList = new ArrayList<String>();

public static void main (String [] args) throws JAXBException {

    K k = new K();

    k.myMap.put("dog", "barks");
    k.myMap.put("cat", "meows");

    k.myMap2.put("bird", "tweets");
    k.myMap2.put("snake", "rattles");

    k.myList.add("first");
    k.myList.add("second");

    k.save();

}

public void save() throws JAXBException {

    JAXBContext jaxbContext = JAXBContext.newInstance(K.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    marshaller.marshal(this, new File("K.xml"));

}
}

The XML produced, i've added empty lines around to show it. 生成了XML,我在周围添加了空行来显示它。

<k>
<myMap>
    <entry>
        <key>cat</key>
        <value>meows</value>
    </entry>
    <entry>
        <key>dog</key>
        <value>barks</value>

   </entry>
</myMap>


<myMap2/>


<myList>first</myList>
<myList>second</myList>

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB(JSR-222)专家组的成员。

Your interpretation of the JAXB spec is correct. 您对JAXB规范的解释是正确的。 I have confirmed the bug you are seeing with the JAXB implementation included in JDK 1.7.0_21 for the Mac. 我已经确认您在Mac的JDK 1.7.0_21中包含的JAXB实现中看到的错误。 Could you enter an issue using the following link: 您能否使用以下链接输入问题:

If you use EclipseLink MOXy as your JAXB provider (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html ) then you get the correct output: 如果将EclipseLink MOXy用作JAXB提供程序(请参阅: http ://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html),那么您将获得正确的输出:

<?xml version="1.0" encoding="UTF-8"?>
<k>
   <myMap>
      <entry>
         <key>cat</key>
         <value>meows</value>
      </entry>
      <entry>
         <key>dog</key>
         <value>barks</value>
      </entry>
   </myMap>
   <myMap2>
      <entry>
         <key>bird</key>
         <value>tweets</value>
      </entry>
      <entry>
         <key>snake</key>
         <value>rattles</value>
      </entry>
   </myMap2>
   <myList>first</myList>
   <myList>second</myList>
</k>

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

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