gpt4 book ai didi

java - XmlBeans.Factory 解析方法的 ClassCastException 不一致

转载 作者:行者123 更新时间:2023-11-28 22:26:22 25 4
gpt4 key购买 nike

我知道这是一个非常的远景,但我已经花了大约两周的时间试图解决这个问题,所以任何指向正确方向的想法都是无价的。

所以,我有一个使用 XmlBeans 的非常古老的应用程序。我的任务是从 Tomcat 7.0.67 迁移到 Tomcat 8.5.11,引入 Spring Sessions 和 Spring Security 而不是基于领域的身份验证。在迁移之前,在本地(MacOS、Oracle JDK 8)和 Heroku(Ubuntu、OpenJDK 8)上,一切都运行良好,但在迁移之后,一切都在我的本地环境中运行,但在 Heroku 上,有时,当应用程序试图解析一个字符串到适当的 XmlBean,发生此 ClassCastException:

java.lang.ClassCastException: foo.bar.2.impl.PreferencesDocumentImpl cannot be cast to foo.bar.1.PreferencesDocument
at foo.bar.1.PreferencesDocument$Factory.parse(Unknown Source)

我有两个由 XmlBeans 类自动生成的类,它们是从两个没有任何命名空间集的 xsd-schemas 生成的。类同名,但位于不同的包中(发生异常的parse方法位于Factory内部类中,其他方法省略):

/*
* An XML document type.
* Localname: Preferences
* Namespace:
* Java type: foo.bar.1.PreferencesDocument
*
* Automatically generated - do not modify.
*/
package foo.bar.1;

public interface PreferencesDocument extends org.apache.xmlbeans.XmlObject {
public static final org.apache.xmlbeans.SchemaType type = (org.apache.xmlbeans.SchemaType)
org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(PreferencesDocument.class.getClassLoader(), "schemaorg_apache_xmlbeans.system.s2D5798E4F4AFDA8394735C8512CDCBC7").resolveHandle("preferencesa8bfdoctype");

public static final class Factory {
public static foo.bar.1.PreferencesDocument parse(java.lang.String xmlAsString) throws org.apache.xmlbeans.XmlException {
return (foo.bar.PreferencesDocument) org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( xmlAsString, type, null );
}
}
}

/*
* An XML document type.
* Localname: Preferences
* Namespace:
* Java type: foo.bar.1.PreferencesDocument
*
* Automatically generated - do not modify.
*/
package foo.bar.1.impl;

public class PreferencesDocumentImpl extends org.apache.xmlbeans.impl.values.XmlComplexContentImpl implements foo.bar.1.PreferencesDocument {
public PreferencesDocumentImpl(org.apache.xmlbeans.SchemaType sType) {
super(sType);
}

private static final javax.xml.namespace.QName PREFERENCES$0 = new javax.xml.namespace.QName("", "Preferences");
}

/*
* An XML document type.
* Localname: Preferences
* Namespace:
* Java type: foo.bar.2.PreferencesDocument
*
* Automatically generated - do not modify.
*/
package foo.bar.2;

public interface PreferencesDocument extends org.apache.xmlbeans.XmlObject {
public static final org.apache.xmlbeans.SchemaType type = (org.apache.xmlbeans.SchemaType)
org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(PreferencesDocument.class.getClassLoader(), "schemaorg_apache_xmlbeans.system.sC8953008EC716AA258D3951B84AB1CB7").resolveHandle("preferencesa8bfdoctype");

public static final class Factory {
public static foo.bar.2.PreferencesDocument parse(java.lang.String xmlAsString) throws org.apache.xmlbeans.XmlException {
return (foo.bar.2.PreferencesDocument) org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( xmlAsString, type, null ); }

}
}

/*
* An XML document type.
* Localname: Preferences
* Namespace:
* Java type: foo.bar.2.PreferencesDocument
*
* Automatically generated - do not modify.
*/
package foo.bar.2.impl;

public class PreferencesDocumentImpl extends org.apache.xmlbeans.impl.values.XmlComplexContentImpl implements foo.bar.2.PreferencesDocument {

public PreferencesDocumentImpl(org.apache.xmlbeans.SchemaType sType) {
super(sType);
}

private static final javax.xml.namespace.QName PREFERENCES$0 =
new javax.xml.namespace.QName("", "Preferences");
}
}

有时,当部署到 Heroku 的应用程序重新启动时,问题消失了,但在再次重新启动后它又回来了。

根据 this ,根本原因是命名空间的缺失导致了冲突。但由于我们的要求,我无法添加或更改 xsds 的命名空间。那么您知道为什么它在本地与 Tomcat 7 一起工作,在本地与 Tomcat 8 一起工作,在 Heroku 上与 Tomcat 7 一起工作,但在 Heroku 上与 Tomcat 8 不工作吗?

提前致谢。

最佳答案

对于以下类别的错误,此答案也出现在顶部,因此我在此处添加我的答案:

class org.apache.xmlbeans.impl.values.XmlComplexContentImpl cannot be cast to class <class you generated a schema from>

这是一个非常奇怪的错误,毕竟为什么会发生这种转换呢?无论如何,这是因为我正在使用 maven-shade-plugin minimizeJar 功能并且它正在删除一些东西(我还不知道是什么),如果我弄清楚我到底需要什么明确地重新添加我会更新这个答案,但现在我只是禁用了 minimizeJar。

它似乎正在删除模式生成代码的 impl 部分。

更新:如果您想继续使用 minimizeJar,请将其添加到阴影插件的过滤器部分:

<filter>                                    
<artifact>org.apache.xmlbeans:xmlbeans</artifact>
<includes>
<include>**</include>
</includes>
</filter>

更新 2:似乎上面的方法并不总是有效,我对这里的新问题很困惑,但重点仍然是 minimizeJar 是非常不稳定的 w.r.t 依赖项

关于java - XmlBeans.Factory 解析方法的 ClassCastException 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43376800/

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