gpt4 book ai didi

java - jaxb 抛出 javax.xml.bind.UnmarshalException : Expected elements are (none) only when running in osgi bundle

转载 作者:行者123 更新时间:2023-11-30 07:27:12 25 4
gpt4 key购买 nike

我想在我的 liberty osgi Web 应用程序中使用 jaxb 解码 xml。

我有一个包含以下代码的 bundle :

InputStream is = new FileInputStream(serviceXmlPath);
JAXBContext jaxbContext = JAXBContext.newInstance(Service.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return jaxbUnmarshaller.unmarshal(is);

其中 Service 类位于同一个包中,并且我使用蓝图将代码的接口(interface)公开为服务。然后我有另一个包含使用它的 servlet 的包。

我对它能够正确解码的核心代码进行了单元测试。然而,当我在 Osgi 包中的 webshpere liberty 8.5.5.8 上运行它时,出现以下异常:

[err] javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Service"). Expected elements are (none)
[err] at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:660)

我检查以确保当我自由运行时它正在读取正确的文件和数据,并且我的单元测试(来自 eclipse 的 junit 4)和服务器都使用相同的 jre。 (本例中为 IBM java 7。)我没有使用 liberty 中的 jaxb 功能,因此它应该使用 java 中的功能。

我不明白为什么代码在使用 osgi 运行时失败。您能建议我还应该检查什么吗?

编辑:

当我打印出类加载器并看到 JAXBContext 有一个与我自己的类不同的类加载器时,我发现这是一个类加载器问题。但是,使用以下代码更改 jaxb 的类加载器:

ClassLoader classloader = MyClass.class.getClassLoader();
jaxbContext = JAXBContext.newInstance(MyClass.class.getName(), classloader);

结果:

[err] javax.xml.bind.JAXBException: Unable to create context
- with linked exception:
[java.lang.reflect.InvocationTargetException]

然后,我将 jaxb.in​​dex 文件添加到包中,并添加以下代码来打印类加载器:

JAXBContext jaxbContext = JAXBContext.newInstance(Service.class);
JAXBContext jaxbContext2 = JAXBContext.newInstance();
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

System.out.println("classloader of Service.class:" + Service.class.getClassLoader().toString());
System.out.println("classloader of JAXBContext:" + JAXBContext.class.getClassLoader());
System.out.println("classloader of jaxbContext:" + jaxbContext.getClass().getClassLoader());
System.out.println("classloader of jaxbContext2:" + jaxbContext2.getClass().getClassLoader());
System.out.println("classloader of jaxbUnmarshaller:" + Unmarshaller.class.getClassLoader());

当我运行它时,我得到:

classloader of Service.class:org.eclipse.osgi.internal.loader.EquinoxClassLoader@b0adb798[DataService:1.0.0.qualifier(id=379)]
classloader of JAXBContext:org.eclipse.osgi.internal.loader.EquinoxClassLoader@271677de[com.ibm.ws.javaee.jaxb.2.2:1.0.11.cl50820151201-1942(id=343)]
classloader of jaxbContext:org.eclipse.osgi.internal.loader.EquinoxClassLoader@b997af78[com.ibm.ws.xlxp.1.5.3:1.0.11.cl50820151201-1942(id=341)]
classloader of jaxbContext2:org.eclipse.osgi.internal.loader.EquinoxClassLoader@b997af78[com.ibm.ws.xlxp.1.5.3:1.0.11.cl50820151201-1942(id=341)]
classloader of jaxbUnmarshaller:org.eclipse.osgi.internal.loader.EquinoxClassLoader@271677de[com.ibm.ws.javaee.jaxb.2.2:1.0.11.cl50820151201-1942(id=343)

请注意,我是否传入该类并不重要,它正在使用自己的类加载器(顺便说一句,这是在自由中启用了 jaxb2.2 的情况下。如果我禁用它,则除了第一个之外的类加载器都会显示为 null,但其他行为相同)

最佳答案

我终于让它工作了。在我的包 list 中,我导入了 javax.xml.bind 包,我认为这就是我所需要的,因为这是我在调用解码的代码中导入的内容。事实证明,我还需要添加 javax.xml.bind.annotation,因为模型类导入了它。

无论如何,感谢斯科特的评论

关于java - jaxb 抛出 javax.xml.bind.UnmarshalException : Expected elements are (none) only when running in osgi bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36665450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com