使用XStream反序列化XML在OSGi(Java 1.4)中不起作用
我创建了一些OSGi包。其中一个具有使用XStream将数据导出到xml的功能。它很好用。当将捆绑包用作库而不是在OSGi上下文中时,再次导入也会起作用 但如果我调用另一个包中的导入,我会得到一个“com.thoughtworks.xstream.converters.ConversionException”,并打印出以下调试信息:使用XStream反序列化XML在OSGi(Java 1.4)中不起作用,java,maven,osgi,xstream,java1.4,Java,Maven,Osgi,Xstream,Java1.4,我创建了一些OSGi包。其中一个具有使用XStream将数据导出到xml的功能。它很好用。当将捆绑包用作库而不是在OSGi上下文中时,再次导入也会起作用 但如果我调用另一个包中的导入,我会得到一个“com.thoughtworks.xstream.converters.ConversionException”,并打印出以下调试信息: ---- Debugging information ---- message : Cannot find class ChildDate
---- Debugging information ----
message : Cannot find class ChildDate
cause-exception : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message : Cannot find class ChildData
class : ChildData
required-type : ChildData
path : /ParentData/ChildData
-------------------------------
message : Could not call ParentData.readObject()
cause-exception : com.thoughtworks.xstream.converters.ConversionException
cause-message : Cannot find class ParentData : Cannot find class ChildData
class : ParentData
required-type : ChildData
path : /ParentData/ChildData
-------------------------------
我认为这是一个类似的问题:或者这个:
所以我试图通过设置类加载器来解决这个问题。但它不起作用
my ParentData类的部分内容:
public class ParentData implements Serializable {
// [...]
private static ClassLoader classLoaderForImport = ParentData.class.getClassLoader();
// [...]
public static void setClassLoaderForImport(ClassLoader classLoaderForImport) {
ParentData.classLoaderForImport = classLoaderForImport;
}
// [...]
public static Lumicon importFromXMLFile(String path) {
return importFromFile(new DomDriver(), path);
}
private static ParentData importFromFile(HierarchicalStreamDriver driver, String path) {
try {
XStream xstream = new XStream(driver);
//set the classloader as the default one won't work in any case
xstream.setClassLoader(ParentData.classLoaderForImport);
xstream.alias("ParentData", classLoaderForImport.loadClass(ParentData.class.getName()));//ParentData.class);
xstream.alias("ChildData", classLoaderForImport.loadClass(ChildData.class.getName()));//ChildData.class);
Reader reader = new FileReader(path);
Object object = xstream.fromXML(reader);
return (ParentData) object;
} catch (ClassNotFoundException ex) {
System.out.println("This did not work.");
} catch (FileNotFoundException e) {
System.out.println("File " + path + " not found.");
}
}
// [...]
}
函数xstream.fromXML(reader)
不起作用,但是classLoaderForImport.loadClass(ChildData.class.getName())
没有失败
这是我从另一个包中对它的称呼:
ParentData.setClassLoaderForImport(ParentData.class.getClassLoader());
data = ParentData.importFromXMLFile(path); // this is where the Exception happens
我还尝试了this.getClass().getClassLoader()
和ChildData.class.getClassLoader()
这可能不起作用,因为这个函数是从第三个bundle调用的吗
更多信息:
- Java版本:1.4(不,我不能升级到1.5或1.6)
- 执行环境:J2SE-1.6
- Maven版本:2.2.1
- 使用OPS4J中的Pax Runner(1.5.0)跑步-
- OSGi:Equinox 3.6.0
- XStream 1.3.1(com.springsource.com.thoughtworks.XStream)
欢迎任何帮助 假设您的问题包含所有相关代码,则问题将通过以下方式解决:
Could not call ParentData.readObject()
您已经声明您的
ParentData实现了可序列化的。尽管Serializable
接口不包含任何方法,但Java序列化和XStream都需要readObject
和writeObject
。对于强制执行这些操作的确切情况,我有点生疏,但我建议您从ParentData
类中删除implements Serializable
。假设您的问题包含所有相关的代码,那么问题可以通过以下方式解决:
Could not call ParentData.readObject()
您已经声明您的ParentData实现了可序列化的。尽管Serializable
接口不包含任何方法,但Java序列化和XStream都需要readObject
和writeObject
。我对强制执行这些操作的确切情况有些生疏,但我建议您从ParentData
类中删除implements Serializable
。“无法调用ParentData.readObject()”此方法存在吗?如果有,我们能看到吗?错误消息中也有一个奇怪的东西,可能是打字错误,但可能在原始消息中有一个线索:“找不到类ChildDate”(奇怪的是,不是原因消息)。@biziclop 1。readObject()不存在。这就是问题所在。2.是的,这是一个输入错误“无法调用ParentData.readObject()”此方法存在吗?如果有,我们能看到吗?错误消息中也有一个奇怪的东西,可能是打字错误,但可能在原始消息中有一个线索:“找不到类ChildDate”(奇怪的是,不是原因消息)。@biziclop 1。readObject()不存在。这就是问题所在。2.是的,这是一个非常简单的打字错误,我不知道这是关于可序列化的。现在我觉得自己很傻:这很简单,我不知道这是关于可序列化的。现在我觉得自己很笨:P