将Java从1.8.0_77更新为1.8.0_121后,JAXB不会解组

时间:2017-01-25 11:24:19

标签: java xml jaxb

昨天我在标题中更新了java like,现在JAXB不再解析xml了。所有对象都是null,似乎没有设置任何东西。

鉴于此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;
    }
}

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();
    }
}

我用它来解组:

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

我有点迷失,因为我没有看到任何理由,也没有在更改日志中找到有关JAXB的任何更改。

我在这里做错了什么?

到目前为止,以下答案并未解决我的问题

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

更新

我现在已将以下依赖项包含在我的项目中

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

它再次起作用。所以新问题 - 为什么? 121 java版本中是否有遗漏/缺陷?

4 个答案:

答案 0 :(得分:3)

我已对此进行了编辑,因为事实证明JRE中的更改在技术上并不是一个错误,但1.8u91和以前的版本更宽松,并允许无效的命名空间XML,如果xml未正确命名空间,则会导致1.8u101 +中断

我在GitHub上创建了一个示例来说明差异。尝试使用1.8u91和1.8u101 +运行两个主电源NoSchemaWithSchema以查看行为差异。

在我的例子中,XML不包含默认命名空间,但元素没有前缀(broker.xml)。这在1.8u91中运行良好,但在1.8u101中失败了。虽然升级破坏了我们的代码,但这在技术上并不是Oracles的错误,因为XML被错误地命名为。

答案 1 :(得分:0)

我遇到了解决jdk 101+版本中空值的解包问题,并通过包含package-info.java和注释@javax.xml.bind.annotation.XmlSchema

解决了这个问题

以下是我的代码

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

import javax.xml.bind.annotation.XmlNsForm;

答案 2 :(得分:0)

Java8较新的版本更加严格,我遇到了同样的问题并发现,我们必须为每个字段指定名称空间,如下所示进行验证 对我有用

@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;

答案 3 :(得分:0)

当使用从elementFormDefault属性设置为qualified的架构生成的类解组基本的,非命名空间的XML时,我遇到了the same issue。只需使用(默认)unqualified值即可为我解决问题:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="unqualified">
...
</xs:schema>