WSO2-ESB服务在某些情况下从端点接收到“空” XML响应。
通常,响应看起来像这样。它包含几个result
:
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:results xmlns:ns2="http://the-endpoint.com/">
<result>some result</result>
<result>some other result</result>
<result>some very different result</result>
</ns2:results>
</S:Body>
</S:Envelope>
然后将其转换为result
的数组并由客户端使用。
{"results": ["some result", ...]}
但是在极少数情况下,端点响应只是一个空结果列表,JAX-WS显然将其转换为空<ns2:results/>
标记。
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:results xmlns:ns2="http://the-endpoint.com/"/>
</S:Body>
</S:Envelope>
[WSO2-ESB然后将其转换为null
对象,这会引起麻烦。
{"results": null}
现在,如果响应为空,如何使WSO2-ESB Web服务将空列表返回为{"results": []}
?
您需要使用XSLT介体将XML处理指令添加到外部元素。
您可以在添加处理说明上参考此博客。 https://medium.com/@CKoutsoumpos/wso2-esb-handle-empty-or-single-item-arrays-during-json-transformation-with-xslt-c1d5a778a548