将Java从1.8.0_45更新为1.8.0_131后,JAXB unmarshal字段为空

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将Java从1.8.0_45更新为1.8.0_131后,JAXB unmarshal字段为空相关的知识,希望对你有一定的参考价值。

我知道这与许多其他帖子几乎相同,例如JAXB doesn't unmarshall after updating java from 1.8.0_77 to 1.8.0_121,但没有一个答案或评论解决了我的问题。

(我还在这里检查了样本https://github.com/KlausGroenbaek/jaxbissue并通读了这个博客http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html

我升级了Java 8,现在使用示例XML文件进行的大量测试都失败了。

unmarshall返回一个所有字段为null的对象。

我不想要一个需要修改package-info或ObjectFactory的解决方案,因为它们会自动重新生成并覆盖所做的任何更改。

我在其他地方听说过Maven选项

<strict>false</strict>

在POM中应该有所帮助,但事实并非如此。

我不确定它是否是相关的差异,但我的架构xsd是在外部项目中定义的。

EDMS.xsd(在Eden Suite项目中)

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:tns="urn:www.edenportal.com.au:exchange:edms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:www.edenportal.com.au:exchange:edms"
            elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">

  <xsd:element name="edmsExchangeData" type="tns:edmsExchange" />

  <xsd:complexType name="edmsExchange">
    <xsd:sequence>
      <xsd:element name="received" type="xsd:dateTime" />
      <xsd:element name="sourceSystem" type="xsd:string" nillable="true" />
      <xsd:element name="rawActivityDataList" type="tns:ActivityData" minOccurs="1" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="ActivityData">
    <xsd:sequence>
      <xsd:element name="networkItemId" type="xsd:long" />
      <xsd:element name="description" type="xsd:string" />
      <xsd:element name="dateFrom" type="xsd:dateTime" />
      <xsd:element name="dateTo" type="xsd:dateTime" />
      <xsd:element name="originalDataSourceFormat" type="tns:OriginalDataSource" />
      <xsd:element name="rawActivityDataItems">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="rawActivityDataItem" minOccurs="1" maxOccurs="unbounded">
              <xsd:complexType>
                <xsd:sequence>
                  <xsd:element name="key" type="xsd:string" />
                  <xsd:element name="value" type="xsd:string" />
                </xsd:sequence>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:simpleType name="OriginalDataSource">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="FuelBilling" />
      <xsd:enumeration value="ElectricityBilling" />
      <xsd:enumeration value="GasBilling" />
      <xsd:enumeration value="WaterBilling" />
      <xsd:enumeration value="ElectricityInterval" />
      <xsd:enumeration value="WasteBilling" />
      <xsd:enumeration value="Paper" />
      <xsd:enumeration value="AirTravel" />
      <xsd:enumeration value="Other" />
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>

unmarshaller代码(在Eden Node项目中):

    private JAXBContext edmsExchangeJaxbContext;

    public EdmsExchangeUnmarshaller() {
        try {
            edmsExchangeJaxbContext = JAXBContext.newInstance("au.com.edenportal.exchange.edms");

...

    public EdmsExchange unmarshallEdmsMessage(MessageBody messageBody) {

...
        Reader inputReader = null;

        if (messageBody.getBinaryContent() != null) {
            LOG.debug("Reading MessageBody as binary... ");
            inputReader = new InputStreamReader(new ByteArrayInputStream(messageBody.getBinaryContent().getValue()));
        } else if (messageBody.getTextContent() != null) {
            LOG.debug("Reading MessageBody as text... ");
            inputReader = new StringReader(messageBody.getTextContent());
        }

        Object unmarshalledObject = null;
        try {

            Unmarshaller unmarshaller = edmsExchangeJaxbContext.createUnmarshaller();
            unmarshalledObject = unmarshaller.unmarshal(inputReader);
            inputReader.close();
        } catch (JAXBException e) {
            throw new ExchangeUnmarshallingException("Could not unmarshall payload", e);
        } catch (IOException io) {
            throw new ExchangeUnmarshallingException("IO error when parsing", io);
        }

        @SuppressWarnings("unchecked")
        EdmsExchange edmsExchange = ((JAXBElement<EdmsExchange>) unmarshalledObject).getValue();
        return edmsExchange;

    }

原始的XML样本测试文件(在Eden Node项目中)在1.8.0_45中工作,但现在不在1.8.0_131中:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<edmsExchangeData xmlns="urn:www.edenportal.com.au:exchange:edms">
    <sourceSystem xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
    <rawActivityDataList>
        <description>Fuel usage</description>
        <dateFrom>2011-02-15T00:00:00.000+10:00</dateFrom>
        <dateTo>2011-02-15T00:00:00.000+10:00</dateTo>
        <originalDataSourceFormat>FuelBilling</originalDataSourceFormat>
        <rawActivityDataItems>
            <rawActivityDataItem>
                <key>id</key>
                <value>1924</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.id</key>
                <value>1503</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.node</key>
                <value>au.gov.vic.fuel</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.cardNumber</key>
                <value>7307992</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.supplierName</key>
                <value>au.com.shell</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>customer.customerName</key>
                <value>POLICE FORCE VICTORIA</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>customer.customerIdentifier</key>
                <value>573537</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.fuelCost</key>
                <value>61.84</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.quantityLitres</key>
                <value>51.54</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.pumpPricePerLitre</key>
                <value>1.33</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.productType</key>
                <value>GASOLINE</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.unitPricePerLitre</key>
                <value>1.32</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.fuelCostInclGST</key>
                <value>68.02</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.totalCostIncTax</key>
                <value>68.02</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.totalTax</key>
                <value>6.18</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.invoiceNumber</key>
                <value>549</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.transactionDate</key>
                <value>2011-02-15 00:00 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.totalCostExTax</key>
                <value>61.84</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.invoiceDate</key>
                <value>2011-02-15 00:00 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.taxRatePercent</key>
                <value>0.1</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>vehicle.odometer</key>
                <value>28694</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>vehicle.regoState</key>
                <value>AUS</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>vehicle.rego</key>
                <value>MCE721H</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.receivedDate</key>
                <value>2011-04-13 11:15 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.insertedDate</key>
                <value>2011-04-13 11:15 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.nodeName</key>
                <value>au.gov.vic.fuel</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.version</key>
                <value>0</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.currencyCode</key>
                <value>AUD</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.correlationId</key>
                <value>e23014f7-2290-44f0-94ff-99a5df6a148e</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.id</key>
                <value>5</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>supplier.id</key>
                <value>5</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>supplier.name</key>
                <value>au.com.shell</value>
            </rawActivityDataItem>
        </rawActivityDataItems>
    </rawActivityDataList>
    <rawActivityDataList>
        <description>Fuel usage</description>
        <dateFrom>2011-02-08T00:00:00.000+10:00</dateFrom>
        <dateTo>2011-02-08T00:00:00.000+10:00</dateTo>
        <originalDataSourceFormat>FuelBilling</originalDataSourceFormat>
        <rawActivityDataItems>
            <rawActivityDataItem>
                <key>id</key>
                <value>2483</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.id</key>
                <value>400</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.node</key>
                <value>au.gov.vic.fuel</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.cardNumber</key>
                <value>7744906</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelCard.supplierName</key>
                <value>au.com.shell</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>customer.customerName</key>
                <value>POLICE FORCE VICTORIA</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>customer.customerIdentifier</key>
                <value>573537</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.fuelCost</key>
                <value>27.7</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.quantityLitres</key>
                <value>23.41</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.pumpPricePerLitre</key>
                <value>1.39</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.productType</key>
                <value>GASOLINE</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.unitPricePerLitre</key>
                <value>1.3</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>fuelDetail.fuelCostInclGST</key>
                <value>30.47</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.totalCostIncTax</key>
                <value>30.47</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.totalTax</key>
                <value>2.77</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.invoiceNumber</key>
                <value>549</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.transactionDate</key>
                <value>2011-02-08 00:00 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.totalCostExTax</key>
                <value>27.7</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.invoiceDate</key>
                <value>2011-02-15 00:00 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>invoice.taxRatePercent</key>
                <value>0.1</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>vehicle.odometer</key>
                <value>22627</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>vehicle.regoState</key>
                <value>AUS</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>vehicle.rego</key>
                <value>XVV759</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.receivedDate</key>
                <value>2011-04-13 11:15 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.insertedDate</key>
                <value>2011-04-13 11:15 +1000</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.nodeName</key>
                <value>au.gov.vic.fuel</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.version</key>
                <value>0</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.currencyCode</key>
                <value>AUD</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.correlationId</key>
                <value>e23014f7-2290-44f0-94ff-99a5df6a148e</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>statement.id</key>
                <value>5</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>supplier.id</key>
                <value>5</value>
            </rawActivityDataItem>
            <rawActivityDataItem>
                <key>supplier.name</key>
                <value>au.com.shell</value>
            </rawActivityDataItem>
        </rawActivityDataItems>
    </rawActivityDataList>
</edmsExchangeData>

XML示例测试文件(在Eden Node项目中) - 在StackOverflow读取后修改后的版本 - 仍然不起作用......:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<e:edmsExchangeData xmlns:e="urn:www.edenportal.com.au:exchange:edms"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="urn:www.edenportal.com.au:exchange:edms EdmsExchange/Edms.xsd ">
    <e:received>2011-04-13T00:00:00.000+10:00</e:received>
    <e:sourceSystem xsi:nil="true"/>
    <e:rawActivityDataList>
        <e:networkItemId>1000000</e:networkItemId>
        <e:description>Fuel usage</e:description>
        <e:dateFrom>2011-02-15T00:00:00.000+10:00</e:dateFrom>
        <e:dateTo>2011-02-15T00:00:00.000+10:00</e:dateTo>
        <e:originalDataSourceFormat>FuelBilling</e:originalDataSourceFormat>
        <e:rawActivityDataItems>
            <e:rawActivityDataItem>
                <e:key>id</e:key>
                <e:value>1924</e:value>
            </e:rawActivityDataItem>
            <e:rawActivityDataItem>
                <e:key>fuelCard.id</e:key>
                <e:value>1503</e:value>
            </e:rawActivityDataItem>
...
            <e:rawActivityDataItem>
                <e:key>supplier.name</e:key>
                <e:value>au.com.shell</e:value>
            </e:rawActivityDataItem>
        </e:rawActivityDataItems>
    </e:rawActivityDataList>
    <e:rawActivityDataList>
        <e:networkItemId>1000001</e:networkItemId>
        <e:description>Fuel usage</e:description>
        <e:dateFrom>2011-02-08T00:00:00.000+10:00</e:dateFrom>
        <e:dateTo>2011-02-08T00:00:00.000+10:00</e:dateTo>
        <e:originalDataSourceFormat>FuelBilling</e:originalDataSourceFormat>
        <e:rawActivityDataItems>
            <e:rawActivityDataItem>
                <e:key>id</e:key>
                <e:value>2483</e:value>
            </e:rawActivityDataItem>
            <e:rawActivityDataItem>
                <e:key>fuelCard.id</e:key>
                <e:value>400</e:value>
            </e:rawActivityDataItem>
...

            <e:rawActivityDataItem>
                <e:key>supplier.name</e:key>
                <e:value>au.com.shell</e:value>
            </e:rawActivityDataItem>
        </e:rawActivityDataItems>
    </e:rawActivityDataList>
</e:edmsExchangeData>

有谁可以帮助解决其他可能的解决方案?谢谢。

答案

与原始帖子一样,我在使用不同的java版本时也遇到了同样的问题。我添加了以下依赖项并解决了我的问题

    <dependency>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.13.3</version>
    </dependency>

以上是关于将Java从1.8.0_45更新为1.8.0_131后,JAXB unmarshal字段为空的主要内容,如果未能解决你的问题,请参考以下文章

wildfly-9.0.0.Final 在 linux x86 上自动停止。 Fedora 3.14.8-200.fc20。 java版本“1.8.0_45”。我该如何解决?

java版本更新到11仍然显示8 [重复]

「Java并发」 HashMap实现原理及源码分析(Java 1.8.0_101)

在我的 Ubuntu 安装中,Java 的“1.8.0_191”是啥版本?

java_1.8.0_ windows_64 windows10环境安装教程

java.lang.LinkageError Groovy 2.4.14 apache poi 3.17 jdk 1.8.0_92 http-builder 0.7.1