如何在 Liferay 中使用 JAXB,获取 ClassCastException

How to use JAXB in Liferay , Getting ClassCastException

我已经从 xsd 文件创建了 AddSystem.java 和 ObjectFactory.java。尝试创建 XML 时显示错误。我正在使用 liferay 和 java 1.8。让我知道您需要的更多信息。

类加载器类加载器=ObjectFactory.class.getClassLoader();<br> JAXBContext 上下文 = JAXBContext.newInstance("com.package.JaxAddSystem",classLoader);

错误:

javax.xml.bind.JAXBException: Provider class com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/C:/Program%20Files/Java/jdk1.8.0_221/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class to bundleresource://4202.fwk877311379:19/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.      _ - with linked exception:_[javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/C:/Program%20Files/Java/jdk1.8.0_221/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class to bundleresource://4202.fwk877311379:19/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.    ] [Sanitized]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:129)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:307)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:478)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:435)

关于您的问题:

javax.xml.bind.JAXBException: Provider class com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/C:/Program%20Files/Java/jdk1.8.0_221/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class to bundleresource://4202.fwk877311379:19/javax/xml/bind/JAXBContext.class. Please make sure that you are specifying the proper ClassLoader. _ - with linked exception:_[javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/C:/Program%20Files/Java/jdk1.8.0_221/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class to bundleresource://4202.fwk877311379:19/javax/xml/bind/JAXBContext.class. Please make sure that you are specifying the proper ClassLoader. ] [Sanitized] at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:129) at javax.xml.bind.ContextFinder.find(ContextFinder.java:307) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:478) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:435)

这是因为 JAXBContext 是从 Liferay classloader (webapps/ROOT/WEB-INF/lib/jaxb-api.jar) 加载的,但还有一点是 JAXBContext 是从 OSGI 模块加载的 (bundleresource://4202 .fwk877311379:19/javax/xml/bind/JAXBContext.class)

我 运行 过去遇到过这个问题,我发现这个问题的根本原因是 JAXB 库中名为 "com.sun.xml.bind.v2.ContextFactory" 的 class。 class 正在从 Liferay classloader 加载,因为 "com.sun.xml" 包包含在 module.framework.properties.org.osgi.framework.bootdelegation portal.properties 配置中。参见:https://github.com/liferay/liferay-portal/blob/b825851dd2f8b617e3d129ff8815e31825a8cc92/portal-impl/src/portal.properties#L7759-L7786

为了解决这个问题,您可以在您的门户中添加javax.xml.*module.framework.properties.org.osgi.framework.bootdelegation属性-ext.properties.

因此您必须将 module.framework.properties.org.osgi.framework.bootdelegation 的默认值复制到您的门户 -ext.properties 并向其附加 javax.xml.*

通过此更改,JAXB classes 将始终从 Liferay classloader 加载,避免所有 ClassCastException 问题。