【问题标题】:JBoss EAP 6.3 / Castor 1.0 - MappingException: Could not find the classJBoss EAP 6.3 / Castor 1.0 - MappingException: 找不到类
【发布时间】:2017-02-15 20:39:42
【问题描述】:

将以前的 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}

(Marshaller 正在尝试将对象序列化为 XML。)

我没有发现明显的类路径问题。这个使用 JBoss Developer Studio 编译良好的应用程序似乎没有理由在运行时失败。

问题出现在 mapping.xml 文件中描述的第一个 Java 类上——不是我尝试序列化的类,除非碰巧它恰好是 mapping.xml 文件中的第一个类。

可能是什么问题,有什么解决方案?

【问题讨论】:

    标签: java jboss-eap-6 castor


    【解决方案1】:

    在搜索了 Google 之后,我在 2002 年的这个讨论帖中找到了解决方案:

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

    该讨论主题中的 Per Fred Loney:

    jboss lib castor.jar 驻留在类加载器中,该类加载器是您的 webapp 类加载器的父级。 Castor 依赖于自省并尝试使用其类加载器上下文而不是 webapp 或线程上下文来解析映射的类。因此找不到您映射的应用程序类.... 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
    

    我希望 Fred Loney 的提示对某人有所帮助。我花了一段时间才在网上找到问题和解决方案!

    【讨论】:

      猜你喜欢
      • 2019-05-10
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      相关资源
      最近更新 更多