如何调试 JAXB 解组?
我遇到了 JAXB 解组问题。我认为我已经正确编码了它,但是我的未编组对象返回了空参数。因此,我假设在解组时,JAXB 没有看到它所期望的适当的 XML 结构。但是,我没有收到任何错误消息或抛出任何异常。
无论如何,是否可以逐步完成解组过程,以准确了解无法填充我的对象的位置/原因?
实际的解组代码相当普通:
public <T> T unmarshall(Node node, Class<T> clazz) throws JAXBException {
// Creating an unmarshaller
Unmarshaller u = JAXBContext.newInstance(clazz).createUnmarshaller();
// unmarshal an instance node into Java content
return clazz.cast(u.unmarshal(node, clazz).getValue());
}
但是,当我调用它时,我得到一个返回的 clazz 类型的对象(如预期),但未填充。
我尝试解组的 DOM 对象是由第三方 API 生成的。我已经在解组过程中遇到了一些极其奇怪的行为,这就是为什么我希望能够调试该过程。例如,如果我尝试解组 DOM 对象中的子元素(即:doc.getByElementName("myElement").item(0)),它会默默地失败。但是,如果我将文档转换为字符串,并将其重新导入到新文档中,则它可以很好地转换。
我开始感到非常沮丧,不知道如何调试这个问题。
感谢您的任何见解!
埃里克
I'm running into a problem with JAXB unmarshalling. I think I have it properly coded, but my unmarshalled object returns with null parameters. Consequently, I am assuming that when unmarshalling, JAXB is not seeing the appropriate XML structure that it is expecting. However, I do not get any error messages or any exceptions thrown.
Is there anyway to step through the unmarshalling process to see exactly where/why it is failing to populate my object(s)?
The actual unmarshalling code is fairly mundane:
public <T> T unmarshall(Node node, Class<T> clazz) throws JAXBException {
// Creating an unmarshaller
Unmarshaller u = JAXBContext.newInstance(clazz).createUnmarshaller();
// unmarshal an instance node into Java content
return clazz.cast(u.unmarshal(node, clazz).getValue());
}
However, when I call it, I get an object of type clazz returned (as expected), but unpopulated.
The DOM object that I am trying to unmarshal is generated by a third party API. I have already run into some extremely odd behaviours with the unmarshalling, which is why I would like to be able to debug the process. For instance, if I try to unmarshal a sub-element within the DOM object (ie: doc.getByElementName("myElement").item(0)), it fails silently. However, if I convert the document to a string, and reimport it into a new document, then it converts it fine.
I am starting to get quite frustrated not knowing how to debug this issue.
Thanks for any insights!
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用默认处理程序。
来自javadoc:
这将产生更详细、更有用的调试输出。
You can try using the default handler.
From the javadocs:
This will produce more verbose, useful output for debugging.
您可以采用的一种方法是使用 JAXB 从带注释的类生成 XML 模式。这表示 JAXB 期望输入文档的外观。然后根据此 XML 模式验证您的 XML 文档,看看它是否符合 JAXB 的期望。
One approach you could take is to use JAXB to generate an XML schema from your annotated classes. This represents what JAXB expects the input document to look like. Then validate your XML document against this XML schema to see if it conforms to JAXB's expectations.
如果其他人像我一样偶然发现这一点。 https://www.kevinhooke.com/2014/03 /25/debugging-jaxb-unmarshalling-issues/
这篇博文至关重要
将以下内容传递到您的启动命令中
然后添加以下 EventHandler。
希望有帮助。在我的 jetty Web 应用程序中,这会导致明显的错误出现,并且我很容易发现命名空间问题。
Should anyone else stumble on this like I did. https://www.kevinhooke.com/2014/03/25/debugging-jaxb-unmarshalling-issues/
This blog post was essential
Pass the following into your start command
Then add the following EventHandler.
hope that helps. In my jetty web app, this enables clear errors to bubble up and I found the namespace issue quite easily.