gpt4 book ai didi

java - 忽略 Apache Axis 1.4 中意外元素的任何变通方法?

转载 作者:太空狗 更新时间:2023-10-29 22:47:48 24 4
gpt4 key购买 nike

问题之前问过"Apache AXIS Ignore/Skip additional element while parsing" 2012 年 Apache Axis 2。Axis 1.4 还没有解决方法吗?

问题定义

例如;

1-我们在 wsdl 中有一个 soap 响应定义('ResponseGetCustomerInfo')[with Axis 1.4]:

...
<xs:element name="ResponseGetCustomerInfo">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:CustomerID"/>
<xs:element ref="ns1:CustomerUsername"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CustomerID" type="xs:integer"/>
<xs:element name="CustomerUsername" type="xs:string"/>
...

2- 很高兴看到响应是可解析的,当我们得到这样的结果时:

<?xml version="1.0" encoding="utf-8" ?> 
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ResponseGetCustomerInfo xmlns="http://tempUri.org/">
<CustomerID>1</CustomerID>
<CustomerUsername>raki</CustomerUsername>
</ResponseGetCustomerInfo>
</soap:Body>
</soap:Envelope>

3-一段时间后,我们的服务提供商更改了服务响应并向响应添加了新的输出字段,我们不知道何时或为何

<?xml version="1.0" encoding="utf-8" ?> 
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ResponseGetCustomerInfo xmlns="http://tempUri.org/">
<CustomerID>1</CustomerID>
<CustomerUsername>raki</CustomerUsername>
<CustomerName>Raki</CustomerName>
<CustomerSurname>Bandao</CustomerSurname>
</ResponseGetCustomerInfo>
</soap:Body>
</soap:Envelope>

4- 新响应理论上与旧版本兼容,因为没有字段既未删除也未更改。但是 Axis 无法解析响应:

"SAXException: Invalid Element ... "

我不想再次更新 wsdl 并重新生成 Web 服务客户端。那么,有什么办法可以跳过响应中的“意外[新添加]元素”吗?或任何解决方法?

我尝试了很多方法,但还没有找到任何解决方案。

最佳答案

由于糟糕的供应商编写这些服务,我们总是会遇到这种情况。

因此,不幸的是,没有办法使用 WSDL2JAVA 的参数,但是有一个解决方法,您将至少重新生成一次 stub :

  1. xs:sequence 替换为 xs:all。这允许元素以任何顺序返回,并有助于修复很多情况,以及生成的 stub 代码,这使得步骤更容易
  2. 抱歉,对于每个响应 bean,您从生成的代码(例如 ResponseGetCustomerInfo.java)进入它的类,而不是这样:
while(!reader.isStartElement() && !reader.isEndElement())
reader.next();

if(reader.isStartElement())
// A start element we are not expecting indicates a trailing invalid
// property
throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());

有这个:

while(!reader.isStartElement() && !reader.isEndElement())
reader.next();

// if(reader.isStartElement())
// A start element we are not expecting indicates a trailing invalid
// property

// The below is commented, to prevent unexpected result parameters from causing an exception
// (As well as the condition above is removed)
// throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());

它已经过测试并且至少比没有解决方案更好。

关于java - 忽略 Apache Axis 1.4 中意外元素的任何变通方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26907856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com