【问题标题】:Remove empty ns attribute from SOAP request using EndpointInterceptor使用 EndpointInterceptor 从 SOAP 请求中删除空的 ns 属性
【发布时间】:2022-09-30 12:15:26
【问题描述】:

有这个遗留系统发送带有空命名空间属性的 SOAP 请求,导致字段值为空,当从 SOAP UI 测试时删除命名空间属性时,它工作正常。

这就是有问题的 SOAP 请求的样子

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"
    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
    <soapenv:Body>
        <ADD xmlns=\"http://www.cc.com/ws\">
            <ID xmlns=\"\">dummy</ID>
            <PWD xmlns=\"\">password</PWD>
        </ADD>
    </soapenv:Body>
</soapenv:Envelope>

我尝试使用WsConfigurerAdapteraddInterceptors 方法添加拦截器,其代码粘贴在下面,但它不起作用

@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
    
    SOAPMessage soapMessage = ((SaajSoapMessage) messageContext.getRequest()).getSaajMessage();
    SOAPBody body = soapMessage.getSOAPBody();
    
    Iterator<SOAPBodyElement> it = body.getChildElements();
    while (it.hasNext()) {
        Object node = it.next();
        if (!isTextNode(node)) {
            com.sun.xml.internal.messaging.saaj.soap.ver1_1.BodyElement1_1Impl e = 
                    (com.sun.xml.internal.messaging.saaj.soap.ver1_1.BodyElement1_1Impl) node;
            
            Iterator<SOAPBodyElement> subItr = e.getChildElements();
            while(subItr.hasNext()) {
                Object subNode = subItr.next();
                if(subNode instanceof com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl) {
                    SOAPElement el = (SOAPElement) subNode;
                
                    el.removeAttribute(\"xmlns\");
                }
            }
            
        }
    }
    
    soapMessage.writeTo(System.out);
    return true;
}

    标签: java spring-boot soap


    【解决方案1】:

    请注意,XML 中的命名空间声明不是属性。不要被类似的语法所迷惑!

    所以我不指望e.removeAttribute 做任何事情,因为没有名称为xmlns 的属性。

    你真正需要做的是改名元素的,例如使用其setElementQName() 方法。

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      相关资源
      最近更新 更多