在不同环境中投射 ElementNSImpl 的问题

Luc*_*cas 3 java spring casting marshalling unmarshalling

我正在构建一个 Java + Spring Web 客户端,该客户端接收 XML 并将其解组为自动生成的类,基于我正在联系的服务提供的模式。

自动生成的代码包含一个返回Element对象的 getter 。为了处理这个对象,我将它转换为ElementNSImpl.

public Element getThing() {
     return thing;
}
Run Code Online (Sandbox Code Playgroud)
ElementNSImpl element = (ElementNSImpl) obj.getThing();
element.doSomething();
Run Code Online (Sandbox Code Playgroud)

但是,我有两个可用的类资源ElementNSImpl

com.sun.org.apache.xerces.internal.dom.ElementNSImpl
Run Code Online (Sandbox Code Playgroud)

org.apache.xerces.dom.ElementNSImpl
Run Code Online (Sandbox Code Playgroud)

我也有两个环境(测试和生产)。测试机运行 CentOS Linux 7 和 OpenJDK 1.8.0_161。生产机器运行 SUSE Linux Enterprise Server 11 SP4 和 Oracle JDK。

我遇到的问题是,当我在测试环境中运行代码时,出现以下异常:

com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to org.apache.xerces.dom.ElementNSImpl
Run Code Online (Sandbox Code Playgroud)

因此,我将我的课程修改为 importcom.sun.org.apache.xerces.internal.dom.ElementNSImpl而不是org.apache.xerces.dom.ElementNSImplvoilà,它开始工作了!然后我将相同的 JAR 上传到生产环境,出现以下异常:

org.apache.xerces.dom.ElementNSImpl cannot be cast to com.sun.org.apache.xerces.internal.dom.ElementNSImpl
Run Code Online (Sandbox Code Playgroud)

似乎每个程序都以不同的方式解组 XML,甚至认为两个代码完全相同。

我还必须告知,正在联系的测试服务器和生产服务器并不相同。他们应该是相同的并且行为方式相同(根据他们的支持团队)。只有不同的数据库。

这个问题可能与我的申请有关吗?或者它可能与我从服务器获取的文件有关?它可能是 OpenJDK 的东西吗?

Luc*_*cas 9

正如@lexicore 指出的那样,我不需要ElementNSImpl. 只需将目标对象更改为 Element并删除演员表即可解决问题。

编辑:我仍然想了解那里发生了什么。