有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从soap头中删除mustUnderstand属性

如何从axis客户端的soap头中删除mustunderstand属性。即使我没有特别设置它,当我设置soap头信息mustUnderstand时,参与者属性也会自动添加到soap消息中。有人知道如何移除它们吗? 我正在使用Axis2 1.4版本的wsdl2java创建我的ws客户端


共 (4) 个答案

  1. # 1 楼答案

    1)是否使用Axis SOAPHeaderElement,如果是,是否将mustUnderstand setter设置为false

    2)由于您使用wsdl2java生成客户机,因此WSDL(或者更准确地说,模式)很可能在SOAP绑定中引用的元素上包含mustUnderstand属性。因此,当wsdlToJava生成客户机代码时,这些属性自然会被添加。有关mustUnderstand属性的描述,请参见here。 如果修改WSDL是不可能的,并且您必须从头中删除这个属性,那么我想您可以尝试使用处理程序来完成

    3)不建议这样做,但是如果您真的必须删除这个属性,那么我想您可以添加一个客户端处理程序来更改头:http://ws.apache.org/axis/java/apiDocs/org/apache/axis/handlers/package-summary.html

  2. # 2 楼答案

    在我的例子中,它可以手动从SOAPHeader中删除属性

    SOAPHeader header = env.getHeader();
    OMChildrenQNameIterator childrenWithName = (OMChildrenQNameIterator) header.getChildrenWithName(omElementauthentication.getQName());
        while (childrenWithName.hasNext()) {
            org.apache.axiom.om.OMElement omElement = (org.apache.axiom.om.OMElement) childrenWithName.next();
            QName mustAnderstandQName = omElement.resolveQName("soapenv:mustUnderstand");
            if (mustAnderstandQName == null) {
                continue;
            }
            OMAttribute mustAnderstandAttribute = omElement.getAttribute(mustAnderstandQName);
                if (mustAnderstandAttribute == null) {
                    continue;
                }
            omElement.removeAttribute(mustAnderstandAttribute);
        }
    
  3. # 3 楼答案

    这些解决方案都不适合我,因为:

    查看“Adding ws-security to wsdl2java generated classes”的答案帮助我编写了一个对我有用的解决方案:

    void addSecurityHeader(Stub stub, final String username, final String password) {
      QName headerName = new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");  // Or any other namespace that fits in your case
      AtomicReference<SOAPHeaderElement> header 
        = new AtomicReference<SOAPHeaderElement>
            (new SOAPHeaderElement(headerName) {                       
               {  
                 SOAPElement utElem = addChildElement("UsernameToken");
                 utElem.addChildElement("Username").setValue(username);
                 utElem.addChildElement("Password").setValue(password);
               }
               @Override
               public void setAttribute(String namespace, String localName, String value) {
                 if (!Constants.ATTR_MUST_UNDERSTAND.equals(localName)) {  // Or any other attribute name you'd want to avoid
                   super.setAttribute(namespace, localName, value);
                 }
               }
            });
      SOAPHeaderElement soapHeaderElement = header.get();
      soapHeaderElement.setActor(null);      // No intermediate actors are involved.
      stub.setHeader(soapHeaderElement);  // Finally, attach the header to the stub
    }
    
  4. # 4 楼答案

    如果要在现有的AXIS客户端中禁用“必须理解”复选框 要在代码中添加以下行:

    _call.setProperty(Call.CHECK_MUST_UNDERSTAND, new Boolean(false));
    

    然后AXIS客户端的MustUnderstandChecker将永远不会被调用