我收到以下设置的 XStream 错误。我一定是疯了。怎么了?
请求类
@XStreamAlias("RequestTO")
public class RequestTO {
@XStreamImplicit
private List<SkuMerchTO> skuNumbers;
...
}
嵌套对象类
@XStreamAlias("skuMerch")
public class SkuMerchTO {
@XStreamAlias("skuNumber")
@XStreamAsAttribute
private Integer skuNumber;
@XStreamAlias("dept")
@XStreamAsAttribute
private Short department;
@XStreamAlias("class")
@XStreamAsAttribute
private Short cls;
@XStreamAlias("subClass")
@XStreamAsAttribute
private Short subClass;
...
}
将 XML 解码为对象的 XStream 代码:
XStream stream = new XStream();
stream.processAnnotations(SkuMerchTO.class);
stream.processAnnotations(RequestTO.class);
RequestTO request = (RequestTO)stream.fromXML(requestXml);
XML 输入字符串:
<RequestTO>
<skuMerch skuNumber="123456" dept="1" class="2" subClass="3"/>
<skuMerch skuNumber="234567" dept="4" class="5" subClass="6"/>
</RequestTO>
Stacktrace 中的错误:
----调试信息----
消息:2:2
原因异常:com.thoughtworks.xstream.mapper.CannotResolveClassException
原因消息:2:2
类:[...]RequestTO
required-type :[...]SkuMerchTO
路径:/RequestTO/skuNumberList/skuMerch
行号:3
.-------------------------------------------- -
com.thoughtworks.xstream.converters.ConversionException:2:2:2:2
----调试信息----
消息:2:2
原因异常:com.thoughtworks.xstream.mapper.CannotResolveClassException
原因消息: 2:2
类:[...]RequestTO
所需类型:[..]SkuMerchTO
路径:/RequestTO/skuNumberList/skuMerch
行号:3
.-------------------- ------------
如果我创建对象并执行 toXML,我会得到:
<RequestTO>
<skuMerch skuNumber="0" dept="1" class="2" subClass="2"/>
<skuMerch skuNumber="1" dept="1" class="2" subClass="2"/>
<skuMerch skuNumber="2" dept="1" class="2" subClass="2"/>
</RequestTO>
编辑:有趣的是,如果这样做:
stream.fromXML(stream.toXML(object));
它在 from XML 部分仍然失败!