尝试解组 xml 时出现类转换异常?
尝试在此处绕过类转换异常:
FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);
抛出此异常:
java.lang.ClassCastException: javax.xml.bind.JAXBElement
我不明白这一点 - 因为该类是由 xjc.bat 工具生成的 - 并且它生成的类我根本没有更改 - 所以应该没有这里的转换问题 - 解组器确实应该给我返回一个可以转换为 FooClass 的类。
关于我做错了什么有什么想法吗?
Trying to get past a class cast exception here:
FooClass fooClass = (FooClass ) unmarshaller.unmarshal(inputStream);
throws this exception:
java.lang.ClassCastException: javax.xml.bind.JAXBElement
I don't understand this - as the class was generated by the xjc.bat tool - and the classes it generated I have not altered at all - so there should be no casting problems here - the unmarshaller should really be giving me back a class that CAN be cast to FooClass.
Any ideas as to what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
FooClass
是否具有XmlRootElement
注释? 如果没有,请尝试:这是基于 非官方 JAXB 指南。
Does
FooClass
have theXmlRootElement
annotation? If not, try:That's based on the Unofficial JAXB Guide.
在 JAXBElement 上使用 JAXBIntrospector 来获取 schemaObject,如 >>
请参阅:JAXB何时unmarshaller.unmarshal返回 JAXBElement; 或者 MySchemaObject?
Use JAXBIntrospector on the JAXBElement to get the schemaObject like >>
Refer: when does JAXB unmarshaller.unmarshal returns a JAXBElement<MySchemaObject> or a MySchemaObject?
我今天遇到了同样的问题,在这里看到了答案,做了一些研究,在我看来,最通用的解决方案是使用 JAXBIntrospector。 因此 -
应该写为
或者甚至更好,使其更通用 -
I ran into the same problem today, saw the answers here, did some research and looks to me that the most generic solution is to use JAXBIntrospector. Hence -
should be written as
Or even better, to make it more generic -
有关更完整的解释,请阅读 这篇文章。 事实证明,您的 XSD 必须正确设置,即必须有某个包含所有其他元素的根元素。
For a fuller explanation read this article. It turns out that your XSD must be properly set up, i.e. there must be some root element encompassing all the other elements.
我们花了太多时间摆弄 JAXB 工厂类来满足解组器的要求。 我们了解到,使用解组器而不调用 JAXB 生成的对象工厂可以正常工作。 希望示例代码可以弥补某人的挫败感:
We spent too many hours fidgeting with the JAXB factory class to satisfy the unmarshaller. We've learned that using the unmarshaller without calling the JAXB-generated object factory works alright. Hope the sample code redeems someone's frustration:
我会查看 XML 文件并确保它大致符合您期望看到的内容。
我还临时将代码更改为:
如果第一个失败,则类转换将在 unmarshal 方法内发生,如果成功,那么您可以看到要返回的实际类,然后找出为什么它没有返回你期望它是什么。
I'd look at the XML file and make sure it is roughly what you expect to see.
I'd also temporarily change the code to:
If the first one failes then the class cast is happening inside the unmarshal method, if it succeeds then you can see the actual class that you are getting back and then figure out why it isn't what you expect it to be.
基于同事的预览答案,以防万一有人仍在寻找答案。
我遇到的问题是我的方案的根元素被定义为:
因此我在以下位置收到了强制转换异常:
我所做的是将 try 块的第一行更改为:
这为我解决了问题。
Building on the previews answers from colleagues, just in case anybody is still looking for an answer.
I had the issue of having the root element of my scheme being defined as:
And therefore I was getting a Cast Exception at:
What I did was to change the first line of the try block to:
That resolved the problem for me.
您绝对确定 FooClass 是您传递给它的 xml 输入源的根元素吗? Unmarshall 将返回 xjc 创建的根元素的对象。
Are you absolutely sure FooClass is the root element of the xml input source you passed it? Unmarshall will return an object of the root element created by xjc.
有时,您的 XSD 定义具有多个不同的根元素(例如 WSDL 中定义的 XSD),在这种情况下,生成的类缺少 @XmlRootElement。 因此,正如用户 mbrauh 已经写的那样,您必须获取 JAXBElement 的值。 在我的例子中,我使用:
因此使用泛型可以轻松避免双重类型转换。
Sometimes you have a XSD definition with multiple different root elements (for instance XSD defined in WSDL) and in that case the generated classes are missing @XmlRootElement. So as user mbrauh already wrote you have to get the value of JAXBElement. In my case I used:
So using generics you can easily avoid double type casting.
如果您有访问权限,则可以修改 XSD。
对我来说,当我使用 IDEA 从 XML 生成 XSD 时,就会出现这个问题。
使用此 xml:
IDEA 生成类似的 XSD,而 JAXB 不会生成根元素:
但是,如果您以这种方式修改 XSD(修改您的根元素“schema”以获得
xs:complexType 位于标记 xs:element) 内:
JAXB 将生成根元素!
If you have access and you can modify the XSD.
For me, this issue appends when I generate the XSD from XML with IDEA.
With this xml :
IDEA generate an XSD like that and JAXB won't generate an root element :
BUT, if you modify the XSD in this way (modify your root element "schema" in order to get the
xs:complexType inside the tag xs:element) :
JAXB will generate the root element !
指定@XmlRootElement(name =“specifyName”,namespace =“namespace”)来转换对象。
Specify @XmlRootElement(name="specifyName", namespace="namespace") to transforming object.
我还遇到了“Javax.xml.bind.JAXBElement 无法转换为”错误,并找到了这个非常简单的解决方案:
显然,由于返回了 JAXBElement 类型的对象,因此您需要对其值进行类型转换。
来源:https://forums.oracle.com/thread/1625944
I also encountered the "Javax.xml.bind.JAXBElement cannot be cast to" error and found this very simple solution:
Since, apparently, an object of type JAXBElement is returned, you need to typecast its value instead.
Source: https://forums.oracle.com/thread/1625944
尝试这个:
Try this:
就我而言,当尝试从 SOAPUI 应用程序发送肥皂请愿书时,我收到错误。 我需要将属性“剥离空格”设置为 true 以跳过此错误。
调试收到的内容时,会出现一个包含以下内容的列表:
希望对某人有所帮助。
In my case, I get the error when trying to send a soap petition from the SOAPUI application. I need to set the property 'strip whitespaces' to true to skip this error.
When debug the content received, is a list with the next content:
Hope help someone.