简体   繁体   English

迁移到Java 8后,Jaxb无法解析XML

[英]Jaxb fails to parse XML after moving to java 8

I Am making to move to java 8, and in the process jaxb fails to parse an xml configuration. 我正在转移到java 8,并且在此过程中jaxb无法解析xml配置。

While changing nothing but the JDK, the same code and file is successfully parsed using java 7, but does not parse correctly on java 8. 虽然只更改了JDK,但是使用java 7成功解析了相同的代码和文件,但在java 8上无法正确解析。

Note that there is no exception thrown but the data does not appear in the object that the xml is unmarshald into. 请注意,没有抛出异常,但数据不会出现在xml是unmarshald的对象中。

Any ideas what might cause this very annoying failure. 什么可能导致这个非常恼人的失败的想法。

I have found the cause of the problem. 我找到了问题的原因。

I have not researched the code, but it seems that the code behind JAXB or SAX parser has changed from java7 to 8, and is less forgiving in java 8. (This might also be due to changes in compiler/jvm itself ) 我还没有研究过代码,但似乎JAXB或SAX解析器背后的代码已从java7更改为8,并且在java 8中的代码更少。(这也可能是由于编译器/ jvm本身的更改)

The parsing was done with annotations. 解析是使用注释完成的。 and it was something that looks like the following: 它看起来像以下内容:

 @XmlRootElement(name = "component_configuration") @XmlAccessorType(XmlAccessType.NONE) public class ComponentConfiguration extends Configuration {} @XmlRootElement(name="configuration") @XmlAccessorType(XmlAccessType.NONE) public class Configuration {} 

In the java 7 version this kind of "game" was either permitted of was successfully parsed (by luck if you ask me) but in java 8 it didn't work any more. 在java 7版本中,这种“游戏”被允许成功解析(运气如果你问我)但在java 8中它不再起作用。

I ended writing the model again correctly and everything worked fine. 我再次正确地再次编写模型,一切正常。

Hope this will help someone else thats braking balls over jaxb compatibility. 希望这将有助于其他人在jaxb兼容性上制动球。

I've found another difference between handling Jaxb on Java 7 versus Java 8. I've reduced the issue to a simplified example. 我发现在Java 7上处理Jaxb与Java 8之间存在另一个区别。我已将问题简化为简化示例。 (changed the categories so it's non-work-related etc) When the setter for a List is called by the Unmarshaller: (更改了类别,因此它与非工作相关等)当Unmarshaller调用List的setter时:

  • when run in Java 7 the setter will be called with data in the List 在Java 7中运行时,将使用List中的数据调用setter
  • when run in Java 8 the setter will be called only with an empty List object which apparently gets populated later in the unmarshalling process. 当在Java 8中运行时,将仅使用空的List对象调用setter,该对象显然在稍后的解组过程中被填充。

The difference I experience is that I must not have the setter do any processing on the List, but rather have a process that is invoked only after the overall object is unmarshalled. 我遇到的不同之处在于我不能让setter对List执行任何处理,而是只有在整个对象被解组后才调用的进程。 Or to sum it up, "don't do any processing in the setter". 或者总结一下,“不要在设置器中进行任何处理”。 I've posted the revised example in the question at Difference in JAXB Java 7 versus Java 8 在JAXB Java 7与Java 8的差异问题中发布了修改后的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM