如何修改SOAP XML输出,适配不可变更的Axis Java客户端(返回Map<String,String>)
适配Axis客户端的SOAP响应调整方案
对于使用Axis call.invoke() 方法、期望返回 Map<String,String> 的Java客户端,考虑到你只能修改XML格式且仅能返回一个值的限制,需对现有SOAP响应做以下调整:
- 移除多余层级包裹:删掉
<item>节点,把目标键值对直接放到<promoteDemotePartReturn>节点下;同时因为只需要返回一个值,保留其中一组键值对(比如<status>或者<objectAction>) - 匹配Axis命名空间规则:
<promoteDemotePartReturn>必须保留xmlns="http://xml.apache.org/xml-soap"这个命名空间,Axis会自动把该节点下的直接子元素解析成Map的键值对(元素名作为键,元素文本作为值)
修改后的SOAP响应示例(以保留status为例):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <promoteDemotePartResponse xmlns="http://DefaultNamespace"> <promoteDemotePartReturn xmlns="http://xml.apache.org/xml-soap"> <status>Success</status> </promoteDemotePartReturn> </promoteDemotePartResponse> </soapenv:Body> </soapenv:Envelope>
如果客户端严格遵循Axis标准Map序列化格式,也可以调整为下面的结构:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <promoteDemotePartResponse xmlns="http://DefaultNamespace"> <promoteDemotePartReturn xmlns="http://xml.apache.org/xml-soap"> <entry> <key>status</key> <value>Success</value> </entry> </promoteDemotePartReturn> </promoteDemotePartResponse> </soapenv:Body> </soapenv:Envelope>
这两种格式都能被Axis的call.invoke()正确解析为只包含一个键值对的Map<String,String>,你可以根据客户端实际的解析逻辑选其中一种。
内容的提问来源于stack exchange,提问作者Samir
相关产品推荐
相关产品推荐

