在 JAXB Web 服务上启用传入 XML 验证后,当我发送请求时收到错误消息Invalid index -1
。
我已经检查过何时发生此错误。我正在发送对象,其中一个字段为抽象类型。当此字段为空时,验证通过。发送此对象时,无论设置什么字段,都会发生错误...
抽象类型的定义:
<xs:complexType abstract="true" name="Customer">
<xs:sequence>
<xs:element minOccurs="0" name="id" type="xs:long"/>
<xs:element minOccurs="0" name="address" type="tns:Address"/>
<xs:element minOccurs="0" name="nip" type="xs:string"/>
<xs:element minOccurs="0" name="type" type="xs:string"/>
</xs:sequence>
</xs:complexType>
具体类型的定义:
<xs:complexType name="PersonalCustomer">
<xs:complexContent>
<xs:extension base="tns:Customer">
<xs:sequence>
<xs:element minOccurs="0" name="PESEL" type="xs:string"/>
<xs:element minOccurs="0" name="firstName" type="xs:string"/>
<xs:element minOccurs="0" name="surname" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
在 CXF 启动时生成 WSDL 的类定义(提取):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Customer", namespace = "http://dto.sample.pl/")
@XmlSeeAlso({ PersonalCustomer.class,
CompanyCustomer.class })
public abstract class Customer implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
private Long id;
private Address address = new Address();
private String nip;
private String type;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonalCustomer", namespace = "http://dto.sample.pl/")
public class PersonalCustomer extends Customer {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The PESEL. */
private String PESEL;
/** The first name. */
private String firstName;
/** The surname. */
private String surname;
}
该错误来自解析 XML 中的 URI 时引发的异常:
Caused by: java.lang.IllegalArgumentException: Invalid index -1; current element has only 1 attributes
at com.ctc.wstx.sr.AttributeCollector.throwIndex(AttributeCollector.java:457)[88:woodstox-core-asl:4.0.8]
at com.ctc.wstx.sr.NsAttributeCollector.getURI(NsAttributeCollector.java:305)[88:woodstox-core-asl:4.0.8]
at com.ctc.wstx.sr.BasicStreamReader.getAttributeNamespace(BasicStreamReader.java:585)[88:woodstox-core-asl:4.0.8]
at org.apache.cxf.staxutils.DepthXMLStreamReader.getAttributeNamespace(DepthXMLStreamReader.java:63)[138:org.apache.cxf.bundle:2.3.3.fuse-01-09]
at javax.xml.stream.util.StreamReaderDelegate.getAttributeNamespace(StreamReaderDelegate.java:112)[67:org.apache.servicemix.specs.stax-api-1.0:1.7.0]
at org.apache.cxf.jaxb.JAXBEncoderDecoder$AddXSITypeStreamReader.getAttributeNamespace(JAXBEncoderDecoder.java:123)[138:org.apache.cxf.bundle:2.3.3.fuse-01-09]
at org.apache.cxf.staxutils.DepthXMLStreamReader.getAttributeNamespace(DepthXMLStreamReader.java:63)[138:org.apache.cxf.bundle:2.3.3.fuse-01-09]
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector$1.getURI(StAXStreamConnector.java:254)[68:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_1]
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.fillXMLAttribute(ValidatorHandlerImpl.java:784)[:1.6.0_29]
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.fillXMLAttributes(ValidatorHandlerImpl.java:764)[:1.6.0_29]
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:546)[:1.6.0_29]
at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:96)[68:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_1]
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:242)[68:org.apache.servicemix.bundles.jaxb-impl:2.2.1.1_1]
这个定义有问题吗?究竟是什么导致了这种神秘消息的验证错误?