gpt4 book ai didi

java - 添加 XMLAdapter 会导致 JAXBContext.newInstance 方法中出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 05:27:49 28 4
gpt4 key购买 nike

我正在学习 JAXB,想尝试一些适配器。当我向我的非常简单的类添加一个时,它导致 JAXBContext.newInstance() 调用抛出 NullPointerException。请注意,适配器并不是绝对必需的。如果我注释掉 @XmlJavaTypeAdapter(MapTypeAdaptor.class) 注释,代码就可以工作。但这并不能帮助我学习如何使用适配器...将 MapType.class 和 MapTypeEntry.class 添加到 JAXBContext.getInstance() 也没有解决问题。

如果有人对我做错的事情提出建议,我真的很感激。谢谢!

这是我正在编码(marshal)的 Java 对象:

import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.s5a.api.models.jaxb.MapTypeAdaptor;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class TestCollections {

@XmlJavaTypeAdapter(MapTypeAdaptor.class) // <---Adding this line causes the error
private Map<String, Object> oneColor;
public Map<String, Object> getColor() {
return oneColor;
}
public void setColor(Map<String, Object> oneColor) {
this.oneColor = oneColor;
}

public List<String> getListOfColors() {
return listOfColors;
}

public void setListOfColors(List<String> listOfColors) {
this.listOfColors = listOfColors;
}

private List<String> listOfColors;


}

这是适配器:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapTypeAdaptor extends XmlAdapter<MapType, Map<String, Object>> {

@Override
public MapType marshal(Map<String, Object> map) throws Exception {
ArrayList<MapEntryType> entries = new ArrayList<MapEntryType>();
MapType mapType = new MapType();
if (map != null && map.entrySet() != null){
for (Entry<String, Object> entry : map.entrySet()) {
MapEntryType mapEntryType = new MapEntryType();
if (entry != null){
mapEntryType.setKey(entry.getKey());
mapEntryType.setValue(entry.getValue());
}
entries.add(mapEntryType);
}
mapType.setEntries(entries);
}
return mapType;
}

@Override
public Map<String, Object> unmarshal(MapType map) throws Exception {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
if (map != null){
for (MapEntryType entryType : map.getEntries()) {
if (entryType != null){
hashMap.put(entryType.getKey(), entryType.getValue());
}
}
}
return hashMap;
}
}

这是 MapType 类:

import java.util.ArrayList;
import java.util.List;

public class MapType {

private List<MapEntryType> mapEntries = new ArrayList<MapEntryType>();

public List<MapEntryType> getEntries() {
return mapEntries;
}

public void setEntries(List<MapEntryType> mapEntries) {
this.mapEntries = mapEntries;
}
}

这是 MapEntryType 类:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
public class MapEntryType {

@XmlAttribute
private String key;
@XmlValue
private Object value;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public Object getValue() {
return value;
}

public void setValue(Object value) {
this.value = value;
}
}

最后,我的单元测试:

@Test
public void shouldReturnXMLRepresentation() throws Exception {

TestCollections test = new TestCollections();
HashMap<String, Object> color1 = new HashMap<String, Object>();
color1.put("blue", new Integer(50));
HashMap<String, Object> color2 = new HashMap<String, Object>();
color2.put("red", "red is the 2nd color");
ArrayList<Map<String, Object>> colors = new ArrayList<Map<String, Object>>();
colors.add(color1);
colors.add(color2);
test.setColor(color1);

ArrayList<String> listofstrings = new ArrayList<String>();
listofstrings.add("foo");
listofstrings.add("bar");
test.setListOfColors(listofstrings);

String xmlRepresentaion = genXML(test);
assertTrue(xmlRepresentaion != null);
assertTrue(xmlRepresentaion.length() > 0);
}

private String genXML(Object object) throws Exception{
StringWriter writer = new StringWriter();
try {
/* I tried the following, but it also throw an NPE
JAXBContext jc = JAXBContext.newInstance(TestCollections.class, MapType.class, MapTypeEntry.class);
*/
JAXBContext jc = JAXBContext.newInstance(TestCollections.class); //<-- NPE
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, writer);
}catch (Exception e){
System.out.println("Error marshalling: " + e.getMessage());
e.printStackTrace(System.out);
throw e;
}
System.out.println(writer.toString());
return writer.toString();
}

最佳答案

如果您需要存储对象值并且愿意接受稍微不同的 XML 输出,您可以更改 MapEntryType:

@XmlAttribute
private String key;
@XmlValue
private Object value;

至:

@XmlElement
private String key;
@XmlElement
private Object value;

这将产生如下输出:

<entry>
<key>someKey</key>
<value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">someValue</value>
</entry>

而不是:

<entry key="someKey">someValue</entry>

或者,如果您可以将 map 从 Map<String, Object> 更改为至Map<String, String> ,那么您现有的类应该可以工作。

关于java - 添加 XMLAdapter 会导致 JAXBContext.newInstance 方法中出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9508630/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com