JAXBContext.createUnmarshaller().unmarshal 得到的属性 都为空的问题解决

  • 【问题描述】

jdk从1.8.0_044 升级到 1.8.0_120
原先通过JAXBContext.createUnmarshaller().unmarshal 解析xml ,还原成的object 正常,
升级jdk后,还原的object本身不为null,但是属性都为null。
以上问题都出现在单元测试阶段。
运行阶段jdk版本为1.8.0_144 也没有出现这个问题(具体怎么回事没有时间验证)。
网上多数文章说是jdk1.8.0—100之后 JAXBContext.createUnmarshaller() 对xml内容格式要求严格了,导致原先不严格的xml还原不了object,但是我的java model都是通过xjc命令+xsd文件生成的,xml内容也是严格按照要求编写的,不存在格式不对的问题。

  • 【解决方法】

最终解决问题的方法是在pom中添加以下依赖:
org.glassfish.jaxb/jaxb-runtime/2.2.11

【相关资料】

https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/Unmarshaller.html
https://stackoverflow.com/questions/41850384/jaxb-doesnt-unmarshall-after-updating-java-from-1-8-0-77-to-1-8-0-121

https://stackoverflow.com/questions/26413431/which-artifacts-should-i-use-for-jaxb-ri-in-my-maven-project?noredirect=1&lq=1

https://www.ibm.com/support/knowledgecenter/en/SSEQTP_8.5.5/com.ibm.websphere.base.doc/ae/twbs_jaxbmarshalxml.html

https://stackoverflow.com/questions/53177129/incompatible-jaxb-dependencies

猜你喜欢

转载自blog.csdn.net/looyo/article/details/85721719