简体   繁体   English

JBoss EAP 6.3 / Castor 1.0-MappingException:找不到该类

[英]JBoss EAP 6.3 / Castor 1.0 - MappingException: Could not find the class

When moving a formerly WebSphere Java EE application to JBoss EAP 6.3, a runtime exception was thrown when invoking the marshall(Object) method of Castor 1.0's org.exolab.castor.xml.Marshaller object: 将以前的WebSphere Java EE应用程序移至JBoss EAP 6.3时,调用Castor 1.0的org.exolab.castor.xml.Marshaller对象的marshall(Object)方法时,将引发运行时异常:

org.exolab.castor.mapping.MappingException: Nested error: org.exolab.castor.mapping.MappingException: Could not find the class {fully qualified Java class name}

(The Marshaller is trying to serialize an object to XML.) (Marshaller尝试将对象序列化为XML。)

I see no obvious class path issues. 我看不到明显的类路径问题。 There is seemingly no reason why this application, which compiles fine using JBoss Developer Studio, would fail at runtime. 使用JBoss Developer Studio可以正常编译的该应用程序在运行时似乎没有理由失败。

The problem occurs with the first Java class described in the mapping.xml file -- not with the class that I'm trying to serialize, unless by coincidence it happens to be the first class in the mapping.xml file. 该问题发生在mapping.xml文件中描述的第一个Java类中,而不是我要序列化的类,除非碰巧它恰巧是mapping.xml文件中的第一个类。

What might be the problem, and what is a solution? 可能是什么问题,什么是解决方案?

After scouring Google, I found the solution in this discussion thread from 2002: 在搜寻Google之后,我从2002年的讨论线程中找到了解决方案:

https://developer.jboss.org/thread/21611 https://developer.jboss.org/thread/21611

Per Fred Loney in that discussion thread: Per Fred Loney在该讨论线程中:

The jboss lib castor.jar resides in a classloader that is a parent of your webapp classloader. jboss lib castor.jar驻留在类加载器中,该类加载器是webapp类加载器的父级。 Castor relies on introspection and attempts to resolve the mapped classes using its classloader context, not the webapp or thread context. Castor依赖于自省,并尝试使用其类加载器上下文而不是webapp或线程上下文来解析映射的类。 Your mapped application classes are therefore not found.... Castor should use the current thread context classloader by default but it doesn't. 因此,找不到映射的应用程序类。...Castor默认情况下应使用当前线程上下文类加载器,但不会。 This is a failing common to other open source middleware projects that rely on introspection, eg cactus. 对于依赖于内省的其他开源中间件项目(例如仙人掌)来说,这是一个失败的普遍现象。

Following the advice recommended in that thread, I changed my code to, rather than use the default constructor of org.exolab.castor.mapping.Mapping, instead use the constructor that accepts a ClassLoader argument, and I used the ClassLoader of the class of the object that I'm trying to serialize. 按照该线程中建议的建议,我将代码更改为,而不是使用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

I hope Fred Loney's tip helps someone. 我希望弗雷德·洛尼(Fred Loney)的小费能对某人有所帮助。 It took me a while to find the problem and solution online! 我花了一段时间才在网上找到问题和解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM