Java com.thoughtworks.xstream.mapper.CannotResolveClassException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21283052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
com.thoughtworks.xstream.mapper.CannotResolveClassException
提问by Sembrano
This is the frist time I am trying XStream. But when I try to parse my xml file i am getting this exception :
这是我第一次尝试 XStream。但是当我尝试解析我的 xml 文件时,我收到了这个异常:
Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: root
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:79)
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.CachingMapper.realClass(CachingMapper.java:45)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1157)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1141)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1012)
at com.mmm.transport.se.xmleditor.xml.TestXML.test(TestXML.java:20)
at com.mmm.transport.se.xmleditor.domain.Main.main(Main.java:13)
So we can see that the error is on row 20 in my testXML class. And it looks like this :
所以我们可以看到错误在我的 testXML 类中的第 20 行。它看起来像这样:
public class TestXML {
public void test() throws FileNotFoundException {
FileReader reader = new FileReader("xmlFiles/CoreDatamodel.xml");
XStream xstream = new XStream();
xstream.processAnnotations(Properties.class);
xstream.processAnnotations(Parameters.class);
xstream.processAnnotations(ObjType.class);
xstream.processAnnotations(Type.class);
Type data = (Type) xstream.fromXML(reader);
System.out.println(data);
}
}
And row 20 is the this line : xstream.processAnnotations(Parameters.class);
第 20 行是这一行: xstream.processAnnotations(Parameters.class);
@XStreamAlias("param")
public class Parameters {
@XStreamAlias("DATATYPE")
private String datatype;
@XStreamAlias("DESCRIPTION")
private String description;
@XStreamAlias("MIN_NO")
private String min_no;
@XStreamAlias("MAX_NO")
private String max_no;
@XStreamAlias("ORDER1")
private String order1;
@XStreamAlias("NESTED")
private String nested;
@XStreamAlias("DEFAULT1")
private String default1;
@XStreamAlias("FORMAT")
private String format;
@XStreamAlias("PARAMETER")
private String parameter;
public Parameters(String datatype, String description, String min_no,
String max_no, String order1, String nested, String default1,
String format, String parameter) {
super();
setDatatype(datatype);
setDescription(description);
setMin_no(min_no);
setMax_no(max_no);
setOrder1(order1);
setNested(nested);
setDefault1(default1);
setFormat(format);
setParameter(parameter);
} //Getters and setters..
And the xml looks like this :
xml 如下所示:
- <root>
- <info>
<CORE_NAME>DataModel_Core</CORE_NAME>
<CORE_VERSION>..</CORE_VERSION>
<CORE_PRODUCT_ID>...</CORE_PRODUCT_ID>
<ADAPTATION_NAME />
<ADAPTATION_VERSION />
<ADAPTATION_PRODUCT_ID />
</info>
- <type>
<OBJECT_TYPE>data</OBJECT_TYPE>
- <prop>
<DESCRIPTION>Site parameters</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>1</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>10008</HELP>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
- <param>
<PARAMETER>nidRbc</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>RBC identity</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>1</ORDER1>
<NESTED>0</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:16382</FORMAT>
</param>
</type>
Other classes involved looks like this :
涉及的其他类如下所示:
public class Type {
@XStreamImplicit(itemFieldName = "type")
private List types = new ArrayList();
}
public class ObjType {
@XStreamAlias("OBJECT_TYPE")
private String objectType;
public ObjType() {
} // Getters and setters
@XStreamAlias("prop")
public class Properties {
@XStreamAlias("DESCRIPTION")
private String description;
@XStreamAlias("PARENT")
private String parent;
@XStreamAlias("VIRTUAL")
private String virtual;
@XStreamAlias("VISIBLE")
private String visible;
@XStreamAlias("PICTURE")
private String picture;
@XStreamAlias("HELP")
private String help;
@XStreamAlias("MIN_NO")
private String min_no;
@XStreamAlias("MAX_NO")
private String max_no;
@XStreamAlias("NAME_FORMAT")
private String name_format;
public static Properties instance = null;
public static Properties getInstance() {
if (instance == null) {
instance = new Properties();
}
return instance;
}
public Properties() {
} // Getters and Setters.
Whats the cause of this error?
这个错误的原因是什么?
采纳答案by Matthias
The reason you get this exception is because you do not have a representation for the xml elements root and info of your xml.
您收到此异常的原因是您没有 xml 元素根和 xml 信息的表示形式。
So when XStream is reading that xml file it is searching for a class matching the root element. If you do not have an alias / mapping defined, it will try the tag name as a class name.
因此,当 XStream 读取该 xml 文件时,它正在搜索与根元素匹配的类。如果您没有定义别名/映射,它将尝试将标签名称作为类名称。
So, instead of:
所以,而不是:
public class Type {
@XStreamImplicit(itemFieldName = "type")
private List types = new ArrayList();
}
use something like that:
使用类似的东西:
@XStreamAlias("root")
public class Type {
private Info info;
@XStreamImplicit(itemFieldName = "type")
private List types = new ArrayList();
}
private class Info {
// ... define the attributes for the info element here.
}
For your reference I have formatted your xml to make things more readable:
为了您的参考,我已经格式化了您的 xml 以使事情更具可读性:
<root>
<info>
<CORE_NAME>DataModel_Core</CORE_NAME>
<CORE_VERSION>..</CORE_VERSION>
<CORE_PRODUCT_ID>...</CORE_PRODUCT_ID>
<ADAPTATION_NAME />
<ADAPTATION_VERSION />
<ADAPTATION_PRODUCT_ID />
</info>
<type>
<OBJECT_TYPE>data</OBJECT_TYPE>
<prop>
<DESCRIPTION>Site parameters</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>1</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>10008</HELP>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
<param>
<PARAMETER>nidRbc</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>RBC identity</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>1</ORDER1>
<NESTED>0</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:16382</FORMAT>
</param>
</type>
</root>
回答by Yeswanth Devisetty
I was recently facing the same issue. The problem being, in the xml request I didn't give the complete qualified name.
我最近面临同样的问题。问题是,在 xml 请求中我没有给出完整的限定名称。
<FeatureContextRequest>
<featureRequestType>GLOBAL</featureRequestType>
</FeatureContextRequest>
After realizing the issue, I've changed it to
意识到问题后,我将其更改为
<com.xxxx.yyyy.services.features.FeatureContextRequest>
<featureRequestType>GLOBAL</featureRequestType>
</com.xxxx.yyyy.services.features.FeatureContextRequest>
It's working fine from then on.
从那时起它工作正常。