JAXB java.lang.ClassCastException,同时编组整数列表

时间:2018-05-07 14:29:29

标签: java jaxb

我有一个包含整数列表的类。此课程是从下面的XSD创建的。

  <xs:complexType name="Vehicle">
    <xs:sequence>
      <xs:element name="regNum" type="xs:string" />
      <xs:element name="company" type="xs:string" />
      <xs:element name="model" type="xs:string" />
      <xs:element name="lights" type="vehicle:Lights" />
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="Lights">
    <xs:list>
      <xs:simpleType>
        <xs:restriction base="xs:int">
          <xs:minInclusive value="1" />
        </xs:restriction>
      </xs:simpleType>
    </xs:list>
  </xs:simpleType>

这产生了以下类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Vehicle", propOrder = {
    "regNum",
    "company",
    "model",
    "lights"
})
@XmlSeeAlso({
    com.vcc.siig.domain.manual.vehicle.Car.class,
    com.vcc.siig.domain.manual.vehicle.Truck.class
})
public class Vehicle {

    @XmlElement(required = true)
    protected String regNum;
    @XmlElement(required = true)
    protected String company;
    @XmlElement(required = true)
    protected String model;
    @XmlList
    @XmlElement(type = Integer.class)
    @XmlSchemaType(name = "anySimpleType")
    protected List<Integer> lights;

    ...
}

当我从Vehicle Object编组到XML时。我收到以下错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer incompatible with java.lang.String
    at com.sun.xml.internal.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$StringImplImpl.print(RuntimeBuiltinLeafInfoImpl.java:982)
    at com.sun.xml.internal.bind.v2.runtime.reflect.ListTransducedAccessorImpl.print(ListTransducedAccessorImpl.java:115)
    at com.sun.xml.internal.bind.v2.runtime.reflect.DefaultTransducedAccessor.writeLeafElement(DefaultTransducedAccessor.java:66)
    at com.sun.xml.internal.bind.v2.runtime.property.ListElementProperty.serializeBody(ListElementProperty.java:111)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:337)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:680)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:147)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:337)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:575)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:318)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:476)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:255)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:107)
    at com.kishor.temp.MarkX.main(MarkX.java:44)

如果将Vehicle.lights设为null,我会得到XML

我不确定这是我的xsd的错误还是问题。

0 个答案:

没有答案