简体   繁体   English

JAXB 忽略 HashMap 属性?

[英]JAXB ignores HashMap properties?

I'm using the JAXB implementation that comes with J2SE to serialize a bean that contains a HashMap property.我正在使用 J2SE 附带的 JAXB 实现来序列化包含 HashMap 属性的 bean。 I would assume that this should work out of the box since this states我认为这应该是开箱即用的,因为表明

JAXB spec defines a special handling for Map when it's used as a propety of a bean. JAXB 规范定义了 Map 用作 bean 的属性时的特殊处理。 For example, the following bean would produce XMLs like the following: ...例如,以下 bean 将生成如下 XML: ...

This more or less works unless the structure has more than one level, ie the HashMap is a property of a bean that is a property of a bean - like this:除非结构具有多个级别,否则这或多或少有效,即 HashMap 是 bean 的属性,它是 bean 的属性 - 如下所示:

import java.util.HashMap;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.JAXB;

class bean { 
    @XmlElement public HashMap<String,String> map;
}

@XmlRootElement class b2 { 
    @XmlElement public bean b;
}


class foo { 
    public static void main(String args[]) { 
        try { 
            bean b = new bean();
            b.map = new HashMap<String,String>();
            b.map.put("a","b");
            b2 two = new b2();
            two.b=b;
            JAXB.marshal(two, System.out);  

        } catch (Exception e) { 
            System.out.println("Exception: " + e.toString());
        }
    }
}

This outputs <?xml version="1.0" encoding="UTF-8" standalone="yes"?><b2><b><map/></b></b2> instead of a correctly formatted HashMap.这将输出<?xml version="1.0" encoding="UTF-8" standalone="yes"?><b2><b><map/></b></b2>而不是正确格式的 HashMap。 It works if I annotate bean with @XmlRootElement and remove the @XmlElement from map , but I don't see why that should be necessary.如果我用@XmlRootElement注释bean并从map中删除@XmlElement ,它会起作用,但我不明白为什么这应该是必要的。 Is it supposed to be like that?应该是这样吗?

The explanation is given on the website you linked:您链接的网站上给出了解释:

Unfortunately, as of 2.1, this processing is only defined for bean properties and not when you marshal HashMap as a top-level object (such as a value in JAXBElement.) In such case, HashMap will be treated as a Java bean, and when you look at HashMap as a bean it defines no getter/setter property pair, so the following code would produce the following XML: Unfortunately, as of 2.1, this processing is only defined for bean properties and not when you marshal HashMap as a top-level object (such as a value in JAXBElement.) In such case, HashMap will be treated as a Java bean, and when您将 HashMap 视为一个 bean,它没有定义 getter/setter 属性对,因此以下代码将生成以下 XML:

Bean with Map:豆用 Map:

m = new HashMap();
m.put("abc",1);
marshaller.marshal(new JAXBElement(new QName("root"),HashMap.class,m),System.out);

XML representation: XML表示:

<root />

This issue has been recorded as #223 and the fix needs to happen in later versions of the JAXB spec.此问题已记录为 #223,需要在 JAXB 规范的更高版本中进行修复。

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

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