JBoss EAP 6.3/Castor 1.0 - MappingException:找不到类
将以前的WebSphere EE应用程序移动到JBoss EAP 6.3时,在调用Castor 1.0组织的marshall(Object)方法时抛出了运行时异常。 exolab.castor.xml.Marshaller对象:JBoss EAP 6.3/Castor 1.0 - MappingException:找不到类
org.exolab.castor.mapping.MappingException: Nested error: org.exolab.castor.mapping.MappingException: Could not find the class {fully qualified Java class name}
(则Marshaller试图序列化对象到XML)。
我看不出有什么明显的类路径的问题。似乎没有理由说明为什么使用JBoss Developer Studio进行编译的这个应用程序在运行时会失败。
问题发生在mapping.xml文件中描述的第一个Java类 - 不是与我试图序列化的类,除非巧合它碰巧是mapping.xml文件中的第一个类。
什么可能是问题,什么是解决方案?
淘谷歌后,我发现在这个话题的解决方案,从2002年:
https://developer.jboss.org/thread/21611
每弗雷德Loney在讨论帖:
在JBoss的lib castor.jar驻留在一个类加载器,它是您的webapp类加载器的父类。 Castor依靠内省并尝试使用其类加载器上下文来解析映射的类,而不是Web应用程序或线程上下文。因此找不到映射的应用程序类......默认情况下,Castor应该使用当前线程上下文类加载器,但它不会。对于依靠内省的其他开源中间件项目来说,这是一种常见的失败,例如,仙人掌。
继该线程建议的意见,我改变了我的代码,而不是使用org.exolab.castor.mapping.Mapping的默认构造函数,而不是使用接受一个ClassLoader参数的构造,和我用我尝试序列化的对象类的ClassLoader。
Object objectToSerialize = ...
// I previously used Mapping's default constructor
Mapping mapping = new Mapping(objectToSerialize.getClass().getClassLoader());
// Set other properties of mapping, such as entityResolver, mapping file, etc.
StringWriter stringWriter = new StringWriter();
Marshaller marshaller = new Marshaller(stringWriter);
marshaller.setMapping(mapping);
marshaller.marshal(objectToSerialize);
// stringWriter now contains the serialized data from objectToSerialize
我希望弗雷德洛尼的小费可以帮助别人。我花了一段时间在网上找到问题和解决方案!