jaxb.index 和嵌套类(和 OSGi)

问题描述 投票:0回答:2

当我尝试引用 jaxb.index 文件中的嵌套类时,序列化期间会引发异常。如何避免这种情况?

这是在 Eclipse RCP 应用程序中。导致异常的类与创建 JAXB 上下文并启动序列化的插件位于不同的插件中。这些类位于插件的导出包之一中。

类结构如下所示(名称已更改):

@XmlRootElement(name="foo")
@XmlAccessorType (XmlAccessType.FIELD)
public class Foo extends AbstractFoo {
   ...
    @XmlRootElement(name="fooMetric")
    @XmlAccessorType (XmlAccessType.FIELD)
    public static class FooMetric implements IFooMetric {
        ...
    }
}

jaxb.index 文件包含这些:

Foo
Foo.FooMetric

在序列化期间,异常提示使用“OuterClass.InnerClass”——我正在这样做。

javax.xml.bind.JAXBException:加载 com/mypackage/jaxb.index 中列出的类“Foo.FooMetric”时出错,请确保条目可在 CLASSPATH 上访问且格式为“ClassName”或“OuterClass.InnerClass”,而不是“ClassName.class”或“完全限定的ClassName” - 带有链接异常: [java.lang.ClassNotFoundException: com.mypackage.Foo.FooMetric]

javadocs(“jaxb.index 的格式”)还建议 jaxb.index 可以包含 OuterClass.InnerClass 形式的条目。

jaxb.index 文件中出现的类名的约束是:

  • 不得以“.class”结尾。
  • 类名是相对于包含 jaxb.index 文件的包进行解析的。仅允许直接出现在包含 jaxb.index 文件的包中的类。
  • 不允许使用完全限定的类名。相对于当前包的限定类名仅允许指定嵌套类或内部类。

但是,这似乎不起作用。什么才能让它发挥作用?

java jaxb
2个回答
5
投票

我发现的解决方案(通过反复试验)是在 jaxb.index 中使用

OuterClass$InnerClass
而不是
OuterClass.InnerClass
。这使得序列化能够成功完成。

但是,我还没有找到任何推荐此的权威来源。

[我根据 stackoverflow 指南发布此解决方案,但很想看到并接受更好的答案。]


0
投票

对于那些使用camel-jaxb(我这边是2.21.1)有类似问题的人来说,使用

OuterClass$InnerClass
OuterClass.InnerClass
都不起作用。 即使例外表明后者应该有效:

org.apache.camel.FailedToCreateRouteException: 
Failed to create route route1: Route(route1)[[From[direct:xxx]] -> [OnException[[class java... 
because of Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: 
javax.xml.bind.JAXBException: error loading class "IdentifierListSearchRequestType.IdentifierSearch" 
listed in com/.../jaxb.index, 
make sure that entries are accessable on CLASSPATH and of the form "ClassName" or "OuterClass.InnerClass", 
not "ClassName.class" or "fully.qualified.ClassName"

我发现的唯一解决方案是在自己的文件中提取内部类并相应地设置

@XmlType
名称,而不是将其留空

© www.soinside.com 2019 - 2024. All rights reserved.