注意: 我是 Eclipselink
JAXB(MOXy)的 负责人,并且是
JAXB(JSR-222) 专家组的成员。
这是一个非常有趣的用例。JAXB(JSR-222)具有地图和域对象的表示形式,因此有必要考虑一下混合对象的行为。我添加了以下增强请求以引入对此的支持:
- http://bugs.eclipse.org/37640
更新
我们刚刚完成了此增强功能。您可以使用Eclipselink 2.4.0每晚下载,从2012年4月19日开始在以下位置进行尝试:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
该修复程序涉及利用该
super-type属性指定超级类型以覆盖实际超级类型。该
super-type属性以前仅由我们的动态JAXB支持使用。
bindings.xml
<?xml version="1.0"?><xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum10075634"> <java-types> <java-type name="SampleClassA" super-type="java.lang.Object" xml-accessor-type="NONE"> <xml-root-element name="SAMPLE" /> <java-attributes> <xml-attribute java-attribute="someProperty" name="SomeProperty" required="true"/> </java-attributes> </java-type> </java-types></xml-bindings>
委托地图
下面是
DelegatatedMap您的问题中描述的类的实现。
package forum10075634;import java.util.*;public class DelegatedMap<K,V> implements Map<K,V> { private Map<K,V> map; public DelegatedMap() { map = new HashMap<K,V>(); } public DelegatedMap(Map<K,V> map) { this.map = map; } public void clear() { map.clear(); } public boolean containsKey(Object key) { return map.containsKey(key); } public boolean containsValue(Object value) { return map.containsValue(value); } public Set<java.util.Map.Entry<K, V>> entrySet() { return map.entrySet(); } public V get(Object key) { return map.get(key); } public boolean isEmpty() { return map.isEmpty(); } public Set<K> keySet() { return map.keySet(); } public V put(K key, V value) { return map.put(key, value); } public void putAll(Map<? extends K, ? extends V> m) { map.putAll(m); } public V remove(Object key) { return map.remove(key); } public int size() { return map.size(); } public Collection<V> values() { return map.values(); }}SampleClassA
package forum10075634;import java.util.Map;public class SampleClassA extends DelegatedMap<String, Object> { public SampleClassA() { super(); } public SampleClassA(Map<String, Object> m) { super(m); } public void setSomeProperty(String value) { put("somevalue", value); } public String getSomeProperty() { return (String) get("somevalue"); }}jaxb.properties
要将MOXy指定为JAXB提供程序,您需要
jaxb.properties使用以下条目添加一个与域类在同一程序包中调用的文件:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示版
package forum10075634;import java.io.StringReader;import java.util.*;import javax.xml.bind.*;import org.eclipse.persistence.jaxb.JAXBContextFactory;public class Demo { public static void main(String[] args) throws Exception { Map<String, Object> properties = new HashMap<String, Object>(1); properties.put(JAXBContextFactory.ECLIPSElink_OXM_XML_KEY, "forum10075634/bindings.xml"); JAXBContext jc = JAXBContext.newInstance(new Class[] {SampleClassA.class}, properties); StringReader xml = new StringReader("<SAMPLE SomeProperty='Foo'/>"); Unmarshaller unmarshaller = jc.createUnmarshaller(); SampleClassA sampleClassA = (SampleClassA) unmarshaller.unmarshal(xml); System.out.println(sampleClassA.getSomeProperty()); System.out.println(sampleClassA.get("somevalue")); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(sampleClassA, System.out); }}输出量
FooFoo<?xml version="1.0" encoding="UTF-8"?><SAMPLE SomeProperty="Foo"/>



