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

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

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

ClassLoader classLoader=ObjectFactory.class.getClassLoader(); JAXBContext context = 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)
xml jaxb liferay classcastexception
1个回答
0
投票

关于您的问题:

javax.xml.bind.JAXBException:提供程序类com.sun.xml.bind.v2.ContextFactory无法实例化:javax.xml.bind.JAXBException:ClassCastException:尝试投射jar:file:/ C:/ Program将%20Files / Java / jdk1.8.0_221 / jre / lib / rt.jar!/javax/xml/bind/JAXBContext.class绑定到bundleresource://4202.fwk877311379:19 / javax / xml / bind / JAXBContext.class。请确保您指定了正确的ClassLoader。 _-带有链接的异常:_ [javax.xml.bind.JAXBException:ClassCastException:尝试投射jar:file:/ C:/Program%20Files/Java/jdk1.8.0_221/jre/lib/rt.jar!/ javax /xml/bind/JAXBContext.class到bundleresource://4202.fwk877311379:19 / javax / xml / bind / JAXBContext.class。请确保您指定了正确的ClassLoader。 ] [已消毒]在javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)在javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:129)在javax.xml.bind.ContextFinder.find(ContextFinder.java:307)在javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:478)在javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:435)

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

过去曾遇到此问题,并且发现此问题的根本原因是JAXB库中名为“ com.sun.xml.bind.v2.ContextFactory”的类。该类是从Liferay类加载器加载的,因为module.framework.properties.org.osgi.framework.bootdelegation portal.properties配置中包含“ com.sun.xml”包。请参阅:https://github.com/liferay/liferay-portal/blob/b825851dd2f8b617e3d129ff8815e31825a8cc92/portal-impl/src/portal.properties#L7759-L7786

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

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

通过此更改,将始终从Liferay类加载器中加载JAXB类,避免了所有ClassCastException问题。

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