我有一个代表 Web 服务请求的 XML 文档的 JAXB 对象 (ProductRequest)。假设它看起来像这样:
<ProductRequest>
<getProducts/>
</ProductRequests>
对于响应,JAXB 对象 (ProductResponse) 将表示一个 XML 文档,如下所示:
<ProductResponse>
<productId>1</productId>
<productName>Oranges</productName>
<productId>2</productId>
<productName>Apples</productName>
</ProductResponse>
使用 Spring-WS,我可以使用两种方法向 Web 服务发送请求
使用 JAXB 对象
ProductRequest productRequest = new productRequest();
ProductResponse productResponse = (ProductResponse) webServiceTemplate
.marshalSendAndReceive(productRequest);
使用纯 XML/DOM
DOMResult domresult = new DOMResult();
webServiceTemplate.sendSourceAndReceiveToResult(source, domresult); //source represents the XML document as a DOMSource object
Element responseElement = (Element) domresult.getNode().getFirstChild();
当我尝试这两种方法时,结果是不同的。使用 JAXB 对象的第一种方法的结果是
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProductResponse xmlns="http://mySchema">
<productId>1</productId>
<productName>Oranges</productName>
<productId>2</productId>
<productName>Apples</productName>
</ProductResponse>
使用 XML Dom 对象的第二种方法的结果是(包括命名空间)
<?xml version="1.0" encoding="UTF-8"?>
<ns2:ProductResponse xmlns:ns2="http://mySchema">
<ns2:productId>1</ns2:productId>
<ns2:productName>Oranges</ns2:productName>
<ns2:productId>2</ns2:productId>
<ns2:productName>Apples</ns2:productName>
</ns2:ProductResponse>
用于 Web 服务响应的模式的标头声明为:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:z="http://mySchema"
targetNamespace="http://mySchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
-- Schema elements
</xs:schema>
反应有两个不同
- JAXB 请求响应的第一行包括条目standalone="yes">
- JAXB 版本上的元素不以命名空间为前缀
- 以架构为前缀的元素的响应不应该使用“z”(如架构中定义的那样)而不是ns2吗?
我不明白是什么导致了这种差异,因为它们都调用相同的 Web 服务,该服务基于相同的模式生成响应。有任何想法吗?
XML 内容是相同的,但 XML 格式的差异给我带来了问题,因为我不能使用 String.equals() 来比较两者。