我已经使用SOAPApache Axis2 1.7.8 Web服务生成了Web客户端。
并且我编写了以下CustomStub类,该类由stub类扩展/继承。

public class CustomStub extends Stub {

protected AxisService _service;
protected ArrayList modules = new ArrayList();


protected ServiceClient _serviceClient;

/**
 * Get service client implementation used by this stub.
 *
 * @return service client
 */
public ServiceClient _getServiceClient() {
    return _serviceClient;
}

/**
 * Set service client implementation used by this stub. Once set, the
 * service client is owned by this stub and will automatically be removed
 * from the configuration when use of the stub is done.
 *
 * @param _serviceClient
 */
public void _setServiceClient(ServiceClient _serviceClient) {
    this._serviceClient = _serviceClient;
}

/**
 * Create a SOAP message envelope using the supplied options.
 * TODO generated stub code should use this method, or similar method taking
 * an operation client
 *
 * @param options
 * @return generated
 * @throws SOAPProcessingException
 */
protected static SOAPEnvelope createEnvelope(Options options) throws SOAPProcessingException {
    return getFactory(options.getSoapVersionURI()).getDefaultEnvelope();
}

/**
 * Get Axiom factory appropriate to selected SOAP version.
 *
 * @param soapVersionURI
 * @return factory
 */
protected static SOAPFactory getFactory(String soapVersionURI) {

    if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(soapVersionURI)) {
        return OMAbstractFactory.getSOAP11Factory();
    } else if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(soapVersionURI)) {
        return OMAbstractFactory.getSOAP12Factory();
    } else {
        throw new RuntimeException(Messages
                .getMessage("unknownsoapversion"));
    }
}

/**
 * Finalize method called by garbage collection. This is overridden to
 * support cleanup of any associated resources.
 *
 * @throws Throwable
 */
protected void finalize() throws Throwable {
    super.finalize();
    cleanup();
}

/**
 * Cleanup associated resources. This removes the axis service from the
 * configuration.
 *
 * @throws AxisFault
 */
public void cleanup() throws AxisFault {
    // service is removed from the service client it self.
    _serviceClient.cleanup();
}

/**
 * sets the epr of the service client to given value
 *
 * @param address
 */

protected void setServiceClientEPR(String address) {
    EndpointReference toEPRFromServiceClient = _serviceClient.getOptions().getTo();
    toEPRFromServiceClient.setAddress(address);
}

/**
 * add an http header with name and value to message context
 *
 * @param messageContext
 * @param name
 * @param value
 */
protected void addHttpHeader(MessageContext messageContext,
                             String name,
                             String value) {
    java.lang.Object headersObj = messageContext.getProperty(HTTPConstants.HTTP_HEADERS);
    if (headersObj == null) {
        headersObj = new java.util.ArrayList();
    }
    java.util.List headers = (java.util.List) headersObj;
    Header header = new Header();
    header.setName(name);
    header.setValue(value);
    headers.add(header);
    messageContext.setProperty(HTTPConstants.HTTP_HEADERS, headers);
}

/**
 * sets the propertykey and propertyValue as a pair to operation client
 *
 * @param operationClient
 * @param propertyKey
 * @param propertyValue
 */

protected void addPropertyToOperationClient(OperationClient operationClient,
                                            String propertyKey,
                                            Object propertyValue) {
    operationClient.getOptions().setProperty(propertyKey, propertyValue);
}

protected void addPropertyToOperationClient(OperationClient operationClient,
                                            String propertyKey,
                                            boolean value) {
    addPropertyToOperationClient(operationClient, propertyKey, new Boolean(value));
}

protected void addPropertyToOperationClient(OperationClient operationClient,
                                            String propertyKey,
                                            int value) {
    addPropertyToOperationClient(operationClient, propertyKey, new Integer(value));
}

protected void setMustUnderstand(OMElement headerElement, OMNamespace omNamespace) {
    OMFactory omFactory = OMAbstractFactory.getOMFactory();
    OMAttribute mustUnderstandAttribute =
            omFactory.createOMAttribute(SOAP12Constants.ATTR_MUSTUNDERSTAND, omNamespace,
                                        "true");
    headerElement.addAttribute(mustUnderstandAttribute);
}

protected void addHeader(OMElement omElementToadd,
                         SOAPEnvelope envelop,
                         boolean mustUnderstand){
    SOAPHeaderBlock soapHeaderBlock =
            envelop.getHeader().addHeaderBlock(omElementToadd.getLocalName(),omElementToadd.getNamespace());
   // soapHeaderBlock.setMustUnderstand(mustUnderstand);
    OMNode omNode = null;

    // add child elements
    Iterator children = omElementToadd.getChildElements();
    while (children.hasNext()) {
   // for (Iterator iter = omElementToadd.getChildren(); iter.hasNext();){
         omNode = (OMNode) children.next();
         soapHeaderBlock.addChild(omNode);
    }

    OMAttribute omatribute = null;
    // add attributes
    for (Iterator iter = omElementToadd.getAllAttributes(); iter.hasNext();){
         omatribute = (OMAttribute) iter.next();
         soapHeaderBlock.addAttribute(omatribute);
    }

}

protected void addHeader(OMElement omElementToadd,
                         SOAPEnvelope envelop){
    addHeader(omElementToadd,envelop,false);
}

protected void addAnonymousOperations(){
    RobustOutOnlyAxisOperation robustoutoonlyOperation =
            new RobustOutOnlyAxisOperation(ServiceClient.ANON_ROBUST_OUT_ONLY_OP);
    _service.addOperation(robustoutoonlyOperation);

    OutOnlyAxisOperation outOnlyOperation = new OutOnlyAxisOperation(ServiceClient.ANON_OUT_ONLY_OP);
    _service.addOperation(outOnlyOperation);

    OutInAxisOperation outInOperation = new OutInAxisOperation(ServiceClient.ANON_OUT_IN_OP);
    _service.addOperation(outInOperation);
}

}


但是,当我调用Web服务时,出现以下错误:

log4j:WARN找不到记录器的附加程序(org.apache.axiom.locator.DefaultOMMetaFactoryLocator)。
log4j:WARN请正确初始化log4j系统。
线程“主”中的异常java.util.ConcurrentModificationException:当前节点已使用Iterator#remove()以外的方法删除。
    在org.apache.axiom.om.impl.traverse.OMAbstractIterator.hasNext(OMAbstractIterator.java:67)
    在org.apache.axiom.om.impl.traverse.OMFilterIterator.hasNext(OMFilterIterator.java:54)
    在org.apache.axis2.axis2userguide.CustomStub.addHeader(CustomStub.java:200)
    在org.apache.axis2.axis2userguide.CustomStub.addHeader(CustomStub.java:217)
    在org.apache.axis2.axis2userguide.TripolisPublishingServiceStub.tripolisPublishEmail(TripolisPublishingServiceStub.java:218)上
    在com.abnamro.Driver.main(Driver.java:38)

最佳答案

当您在OMElement上调用addChild时,该方法将更改要作为子节点添加的节点的父节点。
在调用addChild(omNode)的代码的addHeader块中,您将omNode的父级更改为soapHeaderBlock。因此,下一个hasNext调用失败,说明原始结构已被修改。

试试这个代替:

Iterator children = omElementToadd.getChildElements();
while (children.hasNext()) {
     omNode = (OMNode) children.next();
     OMElement clonedEl = omNode.cloneOMElement();
     soapHeaderBlock.addChild(clonedEl);
}

关于java - 使用Apache Axis2 1.7.8生成Web服务客户端,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52474946/

10-12 00:30
查看更多