JAXB doesn't unmarshall after updating java from 1.8.0_77 to 1.8.0_121

后端 未结 4 533
遇见更好的自我
遇见更好的自我 2021-01-06 05:42

Yesterday I updated java like posted in the title, now JAXB doesn\'t parse xml anymore. All objects are simply null, nothing seems to be set.

Given this POJO - List

相关标签:
4条回答
  • 2021-01-06 06:22

    I faced this issue of unmarshelling giving null values in jdk 101+ versions and solved by including package-info.java and annotation @javax.xml.bind.annotation.XmlSchema

    Below is my code

    @javax.xml.bind.annotation.XmlSchema(elementFormDefault=XmlNsForm.QUALIFIED,namespace="http://example.com/api") package org.example;

    import javax.xml.bind.annotation.XmlNsForm;
    
    0 讨论(0)
  • 2021-01-06 06:25

    I faced the same issue when unmarschalling basic, non-namespaced XML using classes generated from a schema with the elementFormDefault property set to qualified. Simply using the (default) unqualified value solved the problem for me :

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               elementFormDefault="unqualified">
    ...
    </xs:schema>
    
    0 讨论(0)
  • 2021-01-06 06:36

    I have edited this, as it turns out that the change in the JRE was not technically a bug, but 1.8u91 and previous versions were more lenient, and allowed invalid namespaced XML, and 1.8u101+ breaks if xml is not correctly namespaced.

    I have created an example on GitHub to illustrate the difference. Try two run the two mains NoSchema and WithSchema using 1.8u91, and 1.8u101+ to see the difference in behaviour.

    In my case the XML contained no default namespace, but the elements were not prefixed with the namespace they belonged to (broker.xml). This worked fine in 1.8u91, but failed in 1.8u101. Although upgrading broke our code, this is technically not Oracles fault, since the XML was incorrectly namespaced.

    0 讨论(0)
  • 2021-01-06 06:36

    Java8 newer version is more strict, I had same issue and found, we have to specify namespace for each field as follow to validate It worked for me

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "ListMatchingProductsResponse", propOrder = {
            "listMatchingProductsResult",
            "responseMetadata"
    })
    @XmlRootElement(name = "ListMatchingProductsResponse", namespace="http://example.com/api)
    public class ListMatchingProductsResponse {
        @XmlElement(name = "ListMatchingProductsResult", namespace="http://example.com/api)
        private ListMatchingProductsResult listMatchingProductsResult;
        @XmlElement(name = "ResponseMetadata", namespace="http://example.com/api)
        private ResponseMetadata responseMetadata;
        @XmlAttribute(name = "xmlns", namespace="http://example.com/api)
        private String xmlns;
    
    0 讨论(0)
提交回复
热议问题