java 使用EndpointInterceptor从SOAP请求中删除空ns属性

ruoxqz4g  于 2023-02-02  发布在  Java
关注(0)|答案(2)|浏览(196)

有这样一个遗留系统,它发送带有空命名空间属性的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;
}
u5rb5r59

u5rb5r591#

注意,XML中的命名空间声明不是属性。不要被类似的语法搞糊涂了!
因此,我不会期望e.removeAttribute做任何事情,因为没有名为xmlns的属性。
您实际需要做的是 * 更改元素的名称 *,例如使用它的setElementQName()方法。

@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.setElementQName(
                        new QName(
                            e.getElementName().getURI(), 
                            el.getElementName().getLocalName()
                        )
                    );
                }
            }
            
        }
    }
    
    soapMessage.writeTo(System.out);
    return true;
}
qc6wkl3g

qc6wkl3g2#

节点;
,如何实现这个代码我得到错误

相关问题