我有一个受this blog post启发而继承的Web服务。
问题紧随其后,当我使用比根xml元素更高级别定义的默认名称空间时,我不断收到解组异常:
无法创建cz.bevas.experiments.webservices.model.ContactInfo的实例
示例:
我在肥皂信封元素上的命名空间定义异常。
<soapenv:Envelope xmlns="http://model.webservices.experiments.bevas.cz" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<updateCustomerRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>TEST</name>
<contactInfo xsi:type="Address">
<address>My street 1</address>
</contactInfo>
</updateCustomerRequest>
</soapenv:Body>
</soapenv:Envelope>
响应
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring xml:lang="en">Unable to create an instance of cz.bevas.experiments.webservices.model.ContactInfo</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
但是,如果名称空间定义位于根有效负载元素上,则请求将成功处理。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<updateCustomerRequest xmlns="http://model.webservices.experiments.bevas.cz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>TEST</name>
<contactInfo xsi:type="Address">
<address>My street 1</address>
</contactInfo>
</updateCustomerRequest>
</soapenv:Body>
</soapenv:Envelope>
响应
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:updateCustomerResponse xmlns:ns2="http://model.webservices.experiments.bevas.cz"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
customer.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://model.webservices.experiments.bevas.cz"
targetNamespace="http://model.webservices.experiments.bevas.cz" elementFormDefault="qualified">
<xs:element name="updateCustomerRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="contactInfo" type="ContactInfo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="updateCustomerResponse">
<xs:complexType>
<xs:sequence />
</xs:complexType>
</xs:element>
<xs:complexType abstract="true" name="ContactInfo">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="Address">
<xs:complexContent>
<xs:extension base="ContactInfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Phone">
<xs:complexContent>
<xs:extension base="ContactInfo">
<xs:sequence>
<xs:element name="phoneNumber" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
第一个请求有效吗?还是应该是一个错误?
请在github上查看此示例的完整源代码。该项目是基于Spring Boot的可运行应用程序。随时尝试。