我正在运行一个简单的 Web 服务,但在构建和验证响应时遇到问题。此问题与命名空间有关。
在我的 WSDL 中,我有:
<wsdl:definitions targetNamespace="http://me.com/mystuff">
<wsdl:types><xs:schema targetNamespace="http://me.com/mystuf">
....
<xs:complexType name="MyResponse">
<xs:all>
<xs:element name="Value" type="xs:boolean"/>
<xs:element name="ResponseString" type="xs:string"/>
</xs:all>
</wsdl:types>
//MyResponse is bound as a response for a soap operation
</wsdl:definitions>
我首先尝试将响应构建为:
String NAMESPACE_URI="http://me.com/mystuff";
Namespace namespace = Namespace.getNamespace("xyz", NAMESPACE_URI);
Element response = new Element(txType + "Response", namespace);
Element value = new Element("Value");
value.setText("true");
response.addContent(value);
Element responseString = new Element("ResponseString");
response.addContent(responseString);
responseString.setText("");
但我会得到:
org.xml.sax.SAXException: Exception in startElement: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
org.jdom.JDOMException:startElement 中的异常:NAMESPACE_ERR:尝试以不正确的命名空间方式创建或更改对象。
所以我在响应中的子元素中添加了命名空间声明:
Element value = new Element("Value", namespace);
Element responseString = new Element("ResponseString", namespace);
但后来我得到:
ERROR PayloadValidatingInterceptor:238 - XML validation error on response: cvc-complex-type.2.4.a: Invalid content was found starting with element 'xyz:Value'. One of '{Value, ResponseString}' is expected.
关于如何解决这个问题的任何想法?