Jaxb package-info ignored when using Java 10

我是研究僧i 提交于 2019-12-06 12:57:09

I'm actually currently facing the same problem on Java 9.

Adding com.sun.xml.bind.backupWithParentNamespace system property makes it work, but I feel this is working around the problem.

I looked at the Java 9 source(have not looked at the Java 10 source yet), and it's inside the java.lang.Package.getPackageInfo() method that it tries to load the package-info class using:

...
String cn = packageName() + ".package-info";
Module module = module();
...
c = loader.loadClass(module, cn);
...

If I substitute it with

...
c = loader.loadClass(cn);
...

it manages to load it fine and all is well.

To me it looks like a JDK bug.

The latest maven-jaxb2-plugin does have the following option to turn off package-level annotations:

<packageLevelAnnotations>false</packageLevelAnnotations>

This resolved my problem.

Other XJC plugins seems to use "-npa" argument to turn off package-level annotations

I have found the following solution for the same issue in my project with JDK 9:

    JAXBContext ctx = JAXBContext.newInstance(YOUR_CLASS.class);
    Unmarshaller unmarshaller = ctx.createUnmarshaller();
    SAXParserFactory sax = SAXParserFactory.newInstance();
    sax.setNamespaceAware(false); // This line is important!
    XMLReader reader = sax.newSAXParser().getXMLReader();
    Source source = new SAXSource(reader, new InputSource(new StringReader(xml)));
    return (YOUR_CLASS) unmarshaller.unmarshal(source);

I have a similar problem with namespaces, unmarshalling with JAXB in a home-made plugin.

I am using Java 11.

I resolved my problem upgrading maven to 3.6.2 (the problem occured with maven 3.5.2). Not sure to be able to explain why it works, but if it helps someone...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!