情况
我正在使用EclipseLink的MOXy,我正在尝试将外部OX映射XML与实现Map接口的类一起使用。但是,每次我尝试创建一个JAXBContext时,我都会得到以下NPE:
Caused by: javax.xml.bind.JAXBException
- with linked exception:
[java.lang.NullPointerException]
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:832)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:143)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:142)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:129)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:93)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:83)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:210)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:336)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at com.example.MOXyOXTest<clinit>(MOXyOXTest.java:59)
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.jaxb.compiler.XMLProcessor.processXML(XMLProcessor.java:202)
at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:145)
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:829)
详情
仅当要映射的类实现java.util.Map接口时,才会出现此问题。如果我映射的类没有实现该接口,那么一切正常。这是我试图映射的类的简化示例:
package com.example;
import java.util.Map;
// This class just wraps a java.util.HashMap
import com.xetus.lib.type.DelegatedMap;
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");
}
}
以下是我想使用的MOXy OX元数据的简化示例:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.example"
xml-mapping-metadata-complete="true">
<java-types>
<java-type name="SampleClassA" xml-accessor-type="NONE">
<xml-root-element name="SAMPLE" />
<java-attributes>
<xml-attribute type="java.lang.String" name="SomeProperty" required="true">
<xml-access-methods get-method="getSomeProperty" set-method="setSomeProperty"/>
</xml-attribute>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
以下是我创建JAXBContext的方法
Map<String, Object> props = new HashMap<String, Object>(1);
List bindings = new ArrayList(1);
bindings.add(new StreamSource(MOXyOXTest.class.getResourceAsStream("test-mappings.xml")));
props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, bindings);
cntxt = JAXBContext.newInstance(new Class[] { SampleClassA.class }, props);
我正在使用EclipseLink版本2.3.2,以防这一点很重要。我也试过版本2.2.1,结果相同。
我的问题
这是我第一次尝试在实现java.util.Map接口的类上使用JAXB而且我很好奇我是否遗漏了一些基本的东西。我不希望OX Mappings使用Map的名称/值对,而是将自定义的getter和setter添加到类中。
这样的配置应该有效吗?
其他详细信息
答案 0 :(得分:2)
注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。
这是一个非常有趣的用例。 JAXB(JSR-222)具有地图和域对象的表示,因此有必要考虑混合对象应该如何表现。我添加了以下增强请求以引入对它的支持:
<强>更新强>
我们刚刚完成了此增强功能。您可以使用2012年4月19日的EclipseLink 2.4.0每晚下载开始从以下位置试用:
该修复涉及利用super-type
属性指定超类型以覆盖真正的超类型。 super-type
属性以前仅由dynamic 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>
<强> DelegatedMap 强>
以下是您问题中描述的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);
}
}
<强>输出强>
Foo
Foo
<?xml version="1.0" encoding="UTF-8"?>
<SAMPLE SomeProperty="Foo"/>