【问题标题】:JAXB doesn't unmarshall after updating java from 1.8.0_77 to 1.8.0_121将 java 从 1.8.0_77 更新到 1.8.0_121 后,JAXB 不会解组
【发布时间】:2017-06-10 13:29:26
【问题描述】:

昨天我更新了标题中发布的java,现在JAXB不再解析xml了。所有对象都只是空,似乎没有设置。

鉴于此 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 版本中是否缺少某些内容/错误?

【问题讨论】:

  • maven-jaxb2-plugin 是一个用于代码生成的 Maven 插件,您不应该将其作为编译或运行时依赖项。
  • @lexicore 我尝试了几种不同的 jaxb 依赖项,但没有一个有效 - 但包括以上一个。虽然我知道它没有任何意义,但我想知道为什么包含它会使它再次工作......
  • maven-jaxb2-plugin 本身依赖于 JAXB 库。所以基本上你从 Maven 插件“继承”依赖项(而不是自己拥有它们)。看到这个:stackoverflow.com/questions/26413431/…
  • JAXB 有类似的问题。首先用jdk1.8.0_102注意到它。以前不需要调查和修复
  • 我也有类似的问题。我正在从 1.8.0_91 升级到 1.8.0_121(以获得letsencrypt 证书支持),但_121 无法解析我的xml(所有字段均为空)。我尝试使用两个 JDK 生成,并使用两个 JDK 进行解析,但解析总是失败,_121。这听起来像是 _121 的解析问题,因为即使使用 _121 生成类,_91 也可以解析 XML。

标签: java xml jaxb


【解决方案1】:

我已经编辑了这个,因为事实证明 JRE 中的更改在技术上不是错误,但是 1.8u91 和以前的版本更加宽松,并且允许无效的命名空间 XML,如果 xml 没有正确命名空间,则 1.8u101+ 中断.

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

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

【讨论】:

  • 当我尝试构建一个独立的示例时,我发现了以下内容:` jaxb2-maven-plugin` 创建了一个package-info.java 和一个ObjectFactory.java。如果我删除包信息(或其@XmlSchema 注释),那么 Jaxb 也适用于 u101+。所以也许旧版本更宽松,而新版本更严格。
  • 救命的答案 - 谢谢。当我们升级到 JRE 1.8.101 并且 no 异常被打印到日志时,我的对象解组开始失败。
【解决方案2】:

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

下面是我的代码

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

import javax.xml.bind.annotation.XmlNsForm;

【讨论】:

    【解决方案3】:

    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;
    

    【讨论】:

      【解决方案4】:

      在使用从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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 2010-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多