gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:56 24 4
gpt4 key购买 nike

我已经使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com