繁体   English   中英

使用 Apache Axis2 1.7.8 生成 Web 服务客户端

[英]Web Services client generation using Apache Axis2 1.7.8

我已经使用Apache Axis2 1.7.8SOAP 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 系统。 线程“main”中的异常 java.util.ConcurrentModificationException:当前节点已使用 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM