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

标签 java soap

我已经使用 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);
}

}

但是当我调用网络服务时,我收到以下错误:

log4j:WARN 找不到记录器 (org.apache.axiom.locator.DefaultOMMetaFactoryLocator) 的附加程序。 log4j:WARN 请正确初始化 log4j 系统。 线程“main”中的异常 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 时,该方法会更改添加为子节点的节点的父节点。 在代码的 addHeader block 中,当您调用 addChild(omNode) 时,您将 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/

相关文章:

java - JPQL查询异常异常AST节点: {vector}

Java - 如何将参数传递给 if 语句 "if "F(x )"found, pass "x"到 F 函数

java - 从 SOAP 消息中检索 Xpath

xml - 如何在 soap UI 中转义符号 (&)

http - SOAP 和 HTTP 协议(protocol)的区别?

java - 将 JSON Base64 字符串转换为 Java 中的 String

java - 二阶泛型的行为似乎与一阶泛型不同

java - 通过 Google Spreadsheet API 添加标题

api - Magento API : Publish a new method in soap V2

java - JAX-WS - 在 Web 方法中获取 SOAP header