编组/解组JAXBElement时的NPE

时间:2015-02-13 06:24:25

标签: jaxb eclipselink moxy

我有以下JAXB映射类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "date_or_period_t", propOrder = {
    "date", "beginDate", "endDate"
})
public class DateOrPeriodT
    implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElementRef(name = "date", type = JAXBElement.class, required = false)
    protected JAXBElement<DateOrDatetimeT> date;
    @XmlElementRef(name = "begin_date", type = JAXBElement.class, required = false)
    protected JAXBElement<Date> beginDate;
    @XmlElementRef(name = "end_date", type = JAXBElement.class, required = false)
    protected JAXBElement<Date> endDate;

    public JAXBElement<DateOrDatetimeT> getDate() { return date; }
    public void setDate(JAXBElement<DateOrDatetimeT> value) { this.date = value; }
    public JAXBElement<Date> getBeginDate() { return beginDate; }
    public void setBeginDate(JAXBElement<Date> value) { this.beginDate = value; }
    public JAXBElement<Date> getEndDate() { return endDate; }
    public void setEndDate(JAXBElement<Date> value) { this.endDate = value; }
}

和相应的xsd:

<xsd:complexType name="date_or_period_t">
  <xsd:choice minOccurs="0">
    <xsd:element name="date" type="date_or_datetime_t" nillable="true"></xsd:element>
    <xsd:sequence>
      <xsd:element name="begin_date" type="xsd:date" nillable="true"></xsd:element>
      <xsd:element name="end_date" type="xsd:date" nillable="true"></xsd:element>
    </xsd:sequence>
  </xsd:choice>
</xsd:complexType>

end_datebegin_date中的一个或两个设置为xsi:nil="true"时,MOXy会抛出NPE:

Caused by: java.lang.NullPointerException
at org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter.convertDataValueToObjectValue(XMLJavaTypeConverter.java:155)
at org.eclipse.persistence.oxm.mappings.converters.XMLConverterAdapter.convertDataValueToObjectValue(XMLConverterAdapter.java:28)
at org.eclipse.persistence.oxm.mappings.converters.XMLConverterAdapter.convertDataValueToObjectValue(XMLConverterAdapter.java:1)
at org.eclipse.persistence.internal.jaxb.JAXBElementConverter.convertDataValueToObjectValue(JAXBElementConverter.java:78)
at org.eclipse.persistence.internal.jaxb.JAXBElementConverter.convertDataValueToObjectValue(JAXBElementConverter.java:56)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.convertDataValueToObjectValue(XMLDirectMapping.java:526)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:296)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:1)
at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.endElement(XMLDirectMappingNodeValue.java:165)
at org.eclipse.persistence.internal.oxm.XMLChoiceObjectMappingNodeValue.endElement(XMLChoiceObjectMappingNodeValue.java:177)
at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.endElement(UnmarshalRecordImpl.java:1050)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:154)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:99)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:86)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:895)
at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:659)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:585)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:140)

datexsi:nil时,如果begin_dateend_date都不是xsi:nil,那么它可以正常工作。

@XmlElementDecl(namespace = "", name = "date", scope = DateOrPeriodT.class)
public JAXBElement<DateOrDatetimeT> createDateOrPeriodTDate(DateOrDatetimeT value) {
    return new JAXBElement<DateOrDatetimeT>(_DateOrPeriodTDate_QNAME, DateOrDatetimeT.class, DateOrPeriodT.class, value);
}
@XmlElementDecl(namespace = "", name = "begin_date", scope = DateOrPeriodT.class)
@XmlJavaTypeAdapter(AdapterDate.class)
public JAXBElement<Date> createDateOrPeriodTBeginDate(Date value) {
    return new JAXBElement<Date>(_DateOrPeriodTBeginDate_QNAME, Date.class, DateOrPeriodT.class, value);
}
@XmlElementDecl(namespace = "", name = "end_date", scope = DateOrPeriodT.class)
@XmlJavaTypeAdapter(AdapterDate.class)
public JAXBElement<Date> createDateOrPeriodTEndDate(Date value) {
    return new JAXBElement<Date>(_DateOrPeriodTEndDate_QNAME, Date.class, DateOrPeriodT.class, value);

0 个答案:

没有答案