baao
baao

Reputation: 73241

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

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 - ListMatchingProductsResponse

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ListMatchingProductsResponse", propOrder = {
        "listMatchingProductsResult",
        "responseMetadata"
})
@XmlRootElement(name = "ListMatchingProductsResponse")
public class ListMatchingProductsResponse {
    @XmlElement(name = "ListMatchingProductsResult")
    private ListMatchingProductsResult listMatchingProductsResult;
    @XmlElement(name = "ResponseMetadata")
    private ResponseMetadata responseMetadata;
    @XmlAttribute(name = "xmlns")
    private String xmlns;

    public String getXmlns() {
        return xmlns;
    }

    public void setXmlns(String xmlns) {
        this.xmlns = xmlns;
    }


    /**
     * Get the value of ListMatchingProductsResult.
     *
     * @return The value of ListMatchingProductsResult.
     */
    public ListMatchingProductsResult getListMatchingProductsResult() {
        return listMatchingProductsResult;
    }

    /**
     * Set the value of ListMatchingProductsResult.
     *
     * @param listMatchingProductsResult The new value to set.
     */
    public void setListMatchingProductsResult(ListMatchingProductsResult listMatchingProductsResult) {
        this.listMatchingProductsResult = listMatchingProductsResult;
    }

    /**
     * Get the value of ResponseMetadata.
     *
     * @return The value of ResponseMetadata.
     */
    public ResponseMetadata getResponseMetadata() {
        return responseMetadata;
    }

    /**
     * Set the value of ResponseMetadata.
     *
     * @param responseMetadata The new value to set.
     */
    public void setResponseMetadata(ResponseMetadata responseMetadata) {
        this.responseMetadata = responseMetadata;
    }
}

And the ListMatchingProductsResult

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="ListMatchingProductsResult", propOrder={
    "products"
})
@XmlRootElement(name = "ListMatchingProductsResult")
public class ListMatchingProductsResult {

    @XmlElement(name="Products")
    private ProductList products;

    /**
     * Get the value of Products.
     *
     * @return The value of Products.
     */
    public ProductList getProducts() {
        return products;
    }

    /**
     * Set the value of Products.
     *
     * @param products
     *            The new value to set.
     */
    public void setProducts(ProductList products) {
        this.products = products;
    }

    /**
     * Check to see if Products is set.
     *
     * @return true if Products is set.
     */
    public boolean isSetProducts() {
        return products != null;
    }


    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("products", products)
                .toString();
    }
}

I use this for unmarshalling:

String s = getResult();
// s is the expected xml string
ListMatchingProductsResponse m = JAXB.unmarshal(new StringReader(s), ListMatchingProductsResponse.class);
log.info(m); // ListMatchingProductsResponse@7164e54
log.info(m.getListMatchingProductsResult()); // null

I'm a bit lost as I don't see any kind of reason for this, nor did I find any changes regarding JAXB in the changelog.

What am I doing wrong here?

The following answers didn't solve my issue so far

JAXB Configuration was broken by upgrading from JDK 1.7 to JDK 1.8 u05 for collections

Unmarshalling jaxB doesn't work, objects null

Unmarshall with JAXB doesn't work

UPDATE

I now have included the following dependency to my project

group: 'org.jvnet.jaxb2.maven2', name: 'maven-jaxb2-plugin', version: '0.13.1'

and it works again. So new question - why is that? Is there something missing / a bug in the 121 java release?

Upvotes: 4

Views: 5375

Answers (4)

Thomas Naskali
Thomas Naskali

Reputation: 581

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>

Upvotes: 0

amoljdv06
amoljdv06

Reputation: 2864

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;

Upvotes: 0

Naresh M
Naresh M

Reputation: 1

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;

Upvotes: 0

Klaus Groenbaek
Klaus Groenbaek

Reputation: 5035

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.

Upvotes: 4

Related Questions