2013-07-08 65 views
2

我需要使用XStream 1.4.4將一堆XML讀入Java域模型。我使用JAXB從XSD生成域類,用看起來合適的@XSteeamAlias替換了一些註釋(儘管目前爲止似乎沒有任何區別)。我還沒有將所有JAXB的東西都剝離出來,因爲我在這裏有很多類,到目前爲止,它似乎太早出錯了。XStream 1.4.4中的ConversionException/CannotResolveClassException

在帶文本內容的第二個XML標記中出現錯誤。不是第一個,奇怪的是。

這裏是我的錯誤:

com.thoughtworks.xstream.converters.ConversionException: dateFrom : dateFrom 
---- Debugging information ---- 
message    : dateFrom 
cause-exception  : com.thoughtworks.xstream.mapper.CannotResolveClassException 
cause-message  : dateFrom 
class    : java.util.ArrayList 
required-type  : java.util.ArrayList 
converter-type  : com.thoughtworks.xstream.converters.collections.CollectionConverter 
path    : /requestedCompany/companyItself/contactDetails/address/dateFrom 
line number   : 7 
class[1]   : nl.foo.domain.ContactDetails 
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter 
class[2]   : nl.foo.domain.YCompanyType 
class[3]   : nl.foo.domain.RequestedCompany 
version    : null 
------------------------------- 
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30) 
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45) 
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29) 
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70) 
at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:79) 
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:72) 
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:66) 
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) 
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134) 
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) 
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1058) 
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1042) 
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:913) 
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:904) 

這裏的XML我試圖解析的開始:

<?xml version="1.0" encoding="ISO-8859-1"?><requestedCompany xmlns="http://www.graydon.nl/XSellence/scorePlus" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<companyItself graydonCompanyIdentificationNumber="891974008"> 
<bla>bla</bla> 
<contactDetails> 
<companyName>WONINGTEXTIEL JAMO --VOORBEELDRAPPORT--</companyName> 
<address status="huidig" type="20"> 
<dateFrom>1995-8-12</dateFrom> 
<street> 
<streetName>HAARLEMMERSTRAAT</streetName> 
<houseNumber>179</houseNumber> 
</street> 

ContactDetails.java看起來是這樣的:

public class ContactDetails { 

@XStreamAlias("companyName") 
protected String companyName; 
@XStreamAlias("tradeName") 
protected List<TradeName> tradeName; 
@XStreamAlias("address") 
protected List<YAddress> address; 
@XStreamAlias("telephoneNumber") 
protected List<YTelephone> telephoneNumber; 
@XStreamAlias("faxNumber") 
protected List<YTelephone> faxNumber; 
} 

我米不知道我需要所有的別名。他們似乎沒有幫助或傷害。

當我刪除dateFrom標記時,在街道上失敗。當我刪除它時,它會在下一個標籤上失敗,等等。有趣的是,它不會在companyName或我添加的不支持的「bla」標籤上失敗。

編輯:看來問題是與列表。它始終是List中對象失敗的第一個元素。地址位於列表中,因此Address中的所有字段都會導致錯誤。

回答

1

回答我自己的後代問題:確實是名單。在每個列表之前,我需要輸入@XStreamImplicit(itemFieldName ="address")

這使得它正確處理列表。