为什么要在XStream上抛出CannotResolveClassException?

时间:2014-11-12 13:20:48

标签: java xstream

我使用以下代码:



@XStreamAlias("ListOfMBDO")
public class XMLListOfMBDO {

	@XStreamImplicit(itemFieldName = "MBDO")
	public List<ModifyBetriebsortDataObject> items = new LinkedList<ModifyBetriebsortDataObject>();

}

public class ModifyBetriebsortDataObject {

	@XStreamAlias("PK")
	public Integer pk;

	@XStreamAlias("NAME")
	public String name;

	public ModifyBetriebsortDataObject(final Integer pk, final String name) {
		this.pk = pk;
		this.name = name;
	}
}


public void loadThis() {
  final String test = "<ListOfMBDO><MBDO><PK>123456</PK><NAME>Test</NAME></MBDO></ListOfMBDO>";
  final XStream _xStream = new XStream(new DomDriver());
  _xStream.processAnnotations(XMLListOfMBDO.class);
  _xStream.processAnnotations(ModifyBetriebsortDataObject.class);
  final XMLListOfMBDO testList = (XMLListOfMBDO) _xStream.fromXML(test);
}
&#13;
&#13;
&#13;

Serializationto XML工作正常。但是反序列化会抛出一个

com.thoughtworks.xstream.mapper.CannotResolveClassException: ....data.XMLListOfMBDO : ....data.XMLListOfMBDO
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:71)
...

为什么?

1 个答案:

答案 0 :(得分:0)

您可以尝试添加

来解决反序列化错误
    _xStream.alias("ListOfMBDO", XMLListOfMBDO.class);