关于使用Axis2 webservice 处理Fault响应时抛org.apache.axis2.AxisFault的分析
使用Axis2这个框架进行webservice协议通讯,期间出了个问题,我(CLIENT)请求后,当服务端返回符合协议的SOAP异常报文,例如<soap:fault> ...
我的程序直接抛org.apache.axis2.AxisFault异常,导致连服务端给我们的报文都没有接收成功。
--请注意,是我连报文都没有接收成功,而不是接收成功后我解析失败了。
- try {
- ServiceClient serviceClient = new ServiceClient();
- Options options = new Options();
- //设置超时时间,单位毫秒
- options.setTimeOutInMilliSeconds(this.wsTimeOut);
- options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
- options.setTo(new EndpointReference(this.wsEndpointAddress));
- options.setSoapVersionURI(org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
- options.setAction(this.wsMethod);
- MessageContext requetMessageContext = new MessageContext();
- SOAPEnvelope env = this.getRequestEnvelope();
- log.info("version : "+ env.getVersion().getEnvelopeURI());
- requetMessageContext.setEnvelope(env);
- OperationClient opClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
- opClient.addMessageContext(requetMessageContext);
- opClient.setOptions(options);
- opClient.execute(true);
- MessageContext rspMC = opClient.getMessageContext("In");
- response = rspMC.getEnvelope().getBody().getFirstElement();
- log.info("应答报文: "+ rspMC.getEnvelope());
- } catch (AxisFault e) {
- this.errRspDesc = "xxxxx";
- log.error("soapDispatch AxisFault!");
- throw e;
- } catch (Exception e) {
- this.errRspDesc = "xxxxxxxxxxxxx!";
- log.error("soapDispatch Exception!");
- throw e;
- }
try {
ServiceClient serviceClient = new ServiceClient();
Options options = new Options();
//设置超时时间,单位毫秒
options.setTimeOutInMilliSeconds(this.wsTimeOut);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setTo(new EndpointReference(this.wsEndpointAddress));
options.setSoapVersionURI(org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setAction(this.wsMethod);
MessageContext requetMessageContext = new MessageContext();
SOAPEnvelope env = this.getRequestEnvelope();
log.info("version : "+ env.getVersion().getEnvelopeURI());
requetMessageContext.setEnvelope(env); OperationClient opClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
opClient.addMessageContext(requetMessageContext);
opClient.setOptions(options);
opClient.execute(true);
MessageContext rspMC = opClient.getMessageContext("In");
response = rspMC.getEnvelope().getBody().getFirstElement();
log.info("应答报文: "+ rspMC.getEnvelope());
} catch (AxisFault e) {
this.errRspDesc = "xxxxx";
log.error("soapDispatch AxisFault!");
throw e;
} catch (Exception e) {
this.errRspDesc = "xxxxxxxxxxxxx!";
log.error("soapDispatch Exception!");
throw e;
}
当执行到发送请求opClient.execute(true);
服务端成功返回格式正常的SOAP异常报文,此时程序直接抛异常,走不到下面rspMC的获取,也就拿不到响应报文。
经过一段时间的查看Axis2源码,终于找到原因。
从opClient.execute(true); 入手,可以看到
- public final void execute(boolean block) throws AxisFault {
- this.sc.setLastOperationContext(this.oc);
- this.executeImpl(block);
- }
public final void execute(boolean block) throws AxisFault {
this.sc.setLastOperationContext(this.oc);
this.executeImpl(block);
}
再看this.executeImpl(block);
- public void executeImpl(boolean block) throws AxisFault {
- if(log.isDebugEnabled()) {
- log.debug("Entry: OutInAxisOperationClient::execute, " + block);
- }
- if(this.completed) {
- throw new AxisFault(Messages.getMessage("mepiscomplted"));
- } else {
- ConfigurationContext cc = this.sc.getConfigurationContext();
- MessageContext mc = this.oc.getMessageContext("Out");
- if(mc == null) {
- throw new AxisFault(Messages.getMessage("outmsgctxnull"));
- } else {
- this.prepareMessageContext(cc, mc);
- if(this.options.getTransportIn() == null && mc.getTransportIn() == null) {
- mc.setTransportIn(ClientUtils.inferInTransport(cc.getAxisConfiguration(), this.options, mc));
- } else if(mc.getTransportIn() == null) {
- mc.setTransportIn(this.options.getTransportIn());
- }
- boolean useAsync = false;
- if(!mc.getOptions().isUseSeparateListener()) {
- Boolean replyTo = (Boolean)mc.getProperty("UseAsyncOperations");
- if(log.isDebugEnabled()) {
- log.debug("OutInAxisOperationClient: useAsyncOption " + replyTo);
- }
- if(replyTo != null) {
- useAsync = replyTo.booleanValue();
- }
- }
- EndpointReference replyTo1 = mc.getReplyTo();
- if(replyTo1 != null) {
- if(replyTo1.hasNoneAddress()) {
- throw new AxisFault(replyTo1.getAddress() + "" + " can not be used with OutInAxisOperationClient , user either " + "fireAndForget or sendRobust)");
- }
- if(replyTo1.isWSAddressingAnonymous() && replyTo1.getAllReferenceParameters() != null) {
- mc.setProperty("includeOptionalHeaders", Boolean.TRUE);
- }
- String customReplyTo = (String)this.options.getProperty(Options.CUSTOM_REPLYTO_ADDRESS);
- if(!Options.CUSTOM_REPLYTO_ADDRESS_TRUE.equals(customReplyTo) && !replyTo1.hasAnonymousAddress()) {
- useAsync = true;
- }
- }
- if(!useAsync && !mc.getOptions().isUseSeparateListener()) {
- if(block) {
- this.send(mc);
- this.completed = true;
- } else {
- this.sc.getConfigurationContext().getThreadPool().execute(new OutInAxisOperationClient.NonBlockingInvocationWorker(this.callback, mc, this.axisCallback));
- }
- } else {
- this.sendAsync(useAsync, mc);
- }
- }
- }
- }
public void executeImpl(boolean block) throws AxisFault {
if(log.isDebugEnabled()) {
log.debug("Entry: OutInAxisOperationClient::execute, " + block);
} if(this.completed) {
throw new AxisFault(Messages.getMessage("mepiscomplted"));
} else {
ConfigurationContext cc = this.sc.getConfigurationContext();
MessageContext mc = this.oc.getMessageContext("Out");
if(mc == null) {
throw new AxisFault(Messages.getMessage("outmsgctxnull"));
} else {
this.prepareMessageContext(cc, mc);
if(this.options.getTransportIn() == null && mc.getTransportIn() == null) {
mc.setTransportIn(ClientUtils.inferInTransport(cc.getAxisConfiguration(), this.options, mc));
} else if(mc.getTransportIn() == null) {
mc.setTransportIn(this.options.getTransportIn());
} boolean useAsync = false;
if(!mc.getOptions().isUseSeparateListener()) {
Boolean replyTo = (Boolean)mc.getProperty("UseAsyncOperations");
if(log.isDebugEnabled()) {
log.debug("OutInAxisOperationClient: useAsyncOption " + replyTo);
} if(replyTo != null) {
useAsync = replyTo.booleanValue();
}
} EndpointReference replyTo1 = mc.getReplyTo();
if(replyTo1 != null) {
if(replyTo1.hasNoneAddress()) {
throw new AxisFault(replyTo1.getAddress() + "" + " can not be used with OutInAxisOperationClient , user either " + "fireAndForget or sendRobust)");
} if(replyTo1.isWSAddressingAnonymous() && replyTo1.getAllReferenceParameters() != null) {
mc.setProperty("includeOptionalHeaders", Boolean.TRUE);
} String customReplyTo = (String)this.options.getProperty(Options.CUSTOM_REPLYTO_ADDRESS);
if(!Options.CUSTOM_REPLYTO_ADDRESS_TRUE.equals(customReplyTo) && !replyTo1.hasAnonymousAddress()) {
useAsync = true;
}
} if(!useAsync && !mc.getOptions().isUseSeparateListener()) {
if(block) {
this.send(mc);
this.completed = true;
} else {
this.sc.getConfigurationContext().getThreadPool().execute(new OutInAxisOperationClient.NonBlockingInvocationWorker(this.callback, mc, this.axisCallback));
}
} else {
this.sendAsync(useAsync, mc);
} }
}
}
进send(mc)方法
- protected MessageContext send(MessageContext msgContext) throws AxisFault {
- MessageContext responseMessageContext = msgContext.getConfigurationContext().createMessageContext();
- responseMessageContext.setServerSide(false);
- responseMessageContext.setOperationContext(msgContext.getOperationContext());
- responseMessageContext.setOptions(new Options(this.options));
- responseMessageContext.setMessageID(msgContext.getMessageID());
- this.addMessageContext(responseMessageContext);
- responseMessageContext.setServiceContext(msgContext.getServiceContext());
- responseMessageContext.setAxisMessage(this.axisOp.getMessage("In"));
- AxisEngine.send(msgContext);
- responseMessageContext.setDoingREST(msgContext.isDoingREST());
- responseMessageContext.setProperty("TRANSPORT_HEADERS", msgContext.getProperty("TRANSPORT_HEADERS"));
- responseMessageContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE, msgContext.getProperty(HTTPConstants.MC_HTTP_STATUS_CODE));
- responseMessageContext.setProperty("TRANSPORT_IN", msgContext.getProperty("TRANSPORT_IN"));
- responseMessageContext.setTransportIn(msgContext.getTransportIn());
- responseMessageContext.setTransportOut(msgContext.getTransportOut());
- this.handleResponse(responseMessageContext);
- return responseMessageContext;
- }
protected MessageContext send(MessageContext msgContext) throws AxisFault {
MessageContext responseMessageContext = msgContext.getConfigurationContext().createMessageContext();
responseMessageContext.setServerSide(false);
responseMessageContext.setOperationContext(msgContext.getOperationContext());
responseMessageContext.setOptions(new Options(this.options));
responseMessageContext.setMessageID(msgContext.getMessageID());
this.addMessageContext(responseMessageContext);
responseMessageContext.setServiceContext(msgContext.getServiceContext());
responseMessageContext.setAxisMessage(this.axisOp.getMessage("In"));
AxisEngine.send(msgContext);
responseMessageContext.setDoingREST(msgContext.isDoingREST());
responseMessageContext.setProperty("TRANSPORT_HEADERS", msgContext.getProperty("TRANSPORT_HEADERS"));
responseMessageContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE, msgContext.getProperty(HTTPConstants.MC_HTTP_STATUS_CODE));
responseMessageContext.setProperty("TRANSPORT_IN", msgContext.getProperty("TRANSPORT_IN"));
responseMessageContext.setTransportIn(msgContext.getTransportIn());
responseMessageContext.setTransportOut(msgContext.getTransportOut());
this.handleResponse(responseMessageContext);
return responseMessageContext;
}
这里就是发送请求并接收响应的地方 再看倒数第二行this.handleResponse(responseMessageContext);
- protected void handleResponse(MessageContext responseMessageContext) throws AxisFault {
- responseMessageContext.setSoapAction((String)null);
- SOAPEnvelope resenvelope;
- if(responseMessageContext.getEnvelope() == null) {
- resenvelope = TransportUtils.createSOAPMessage(responseMessageContext);
- if(resenvelope == null) {
- throw new AxisFault(Messages.getMessage("blockingInvocationExpectsResponse"));
- }
- responseMessageContext.setEnvelope(resenvelope);
- }
- resenvelope = responseMessageContext.getEnvelope();
- if(resenvelope != null) {
- AxisEngine.receive(responseMessageContext);
- if(responseMessageContext.getReplyTo() != null) {
- this.sc.setTargetEPR(responseMessageContext.getReplyTo());
- }
- resenvelope = responseMessageContext.getEnvelope();
- if((resenvelope.hasFault() || responseMessageContext.isProcessingFault()) && this.options.isExceptionToBeThrownOnSOAPFault()) {
- throw Utils.getInboundFaultFromMessageContext(responseMessageContext);
- }
- }
- }
protected void handleResponse(MessageContext responseMessageContext) throws AxisFault {
responseMessageContext.setSoapAction((String)null);
SOAPEnvelope resenvelope;
if(responseMessageContext.getEnvelope() == null) {
resenvelope = TransportUtils.createSOAPMessage(responseMessageContext);
if(resenvelope == null) {
throw new AxisFault(Messages.getMessage("blockingInvocationExpectsResponse"));
} responseMessageContext.setEnvelope(resenvelope);
} resenvelope = responseMessageContext.getEnvelope();
if(resenvelope != null) {
AxisEngine.receive(responseMessageContext);
if(responseMessageContext.getReplyTo() != null) {
this.sc.setTargetEPR(responseMessageContext.getReplyTo());
} resenvelope = responseMessageContext.getEnvelope();
if((resenvelope.hasFault() || responseMessageContext.isProcessingFault()) && this.options.isExceptionToBeThrownOnSOAPFault()) {
throw Utils.getInboundFaultFromMessageContext(responseMessageContext);
}
} }
这时,我们可以看到一个很有趣的方法, if((resenvelope.hasFault() || responseMessageContext.isProcessingFault()) 总算是跟我们的异常报文有关了。跟进去看一下
- public boolean hasFault() {
- QName payloadQName = this.getPayloadQName_Optimized();
- if(payloadQName != null && "Fault".equals(payloadQName.getLocalPart())) {
- String body1 = payloadQName.getNamespaceURI();
- return "http://schemas.xmlsoap.org/soap/envelope/".equals(body1) || "http://www.w3.org/2003/05/soap-envelope".equals(body1);
- } else {
- SOAPBody body = this.getBody();
- return body == null?false:body.hasFault();
- }
- }
public boolean hasFault() {
QName payloadQName = this.getPayloadQName_Optimized();
if(payloadQName != null && "Fault".equals(payloadQName.getLocalPart())) {
String body1 = payloadQName.getNamespaceURI();
return "http://schemas.xmlsoap.org/soap/envelope/".equals(body1) || "http://www.w3.org/2003/05/soap-envelope".equals(body1);
} else {
SOAPBody body = this.getBody();
return body == null?false:body.hasFault();
}
}
可以看到Axis2的内部处理机制,就是一但发现响应报文有Fault节点,它就要抛异常。总算找到源头了 那要如何解决这个问题 我们可以看到 if((resenvelope.hasFault() || responseMessageContext.isProcessingFault()) && this.options.isExceptionToBeThrownOnSOAPFault()) { throw Utils.getInboundFaultFromMessageContext(responseMessageContext); } 这里还有一个判断条件, this.options.isExceptionToBeThrownOnSOAPFault() 当它为TRUE时才抛异常。 这就是options的一个参数,可配置,所以给我们的代码加上
- options.setExceptionToBeThrownOnSOAPFault(false);
options.setExceptionToBeThrownOnSOAPFault(false);
就不抛异常了,能够正常获取并解析响应报文。 总结:一切的害怕源于对代码的神秘,未知,当你把它当成自己写的代码,去反编译,去阅读,那就不会再害怕!
关于使用Axis2 webservice 处理Fault响应时抛org.apache.axis2.AxisFault的分析的更多相关文章
- WebService学习之旅(四)Apache Axis2的安装
一.Axis2简介 Axis2是目前使用较多的WebService引擎,它是Axis1.x的升级版本,不仅支持SOAP1.1和SOAP1.2,而且也提供了对REST风格WebService的支持. A ...
- Apache axis2 + Eclipse 开发 WebService
yd小结注意:1.axis2的2个插件的版本必须与引入的jar包匹配,如果不同则可能报以下错误 “没有实现序列化方法”或 “org.apache.axis2.databinding.utils.wri ...
- Axis2 WebService客户端Axis2调用
第一RPC方式,不生成客户端代码 第二,document方式,不生成客户端代码 第三,用wsdl2java工具,生成客户端方式调用 package samples.quickstart.client; ...
- axis2 webservice jar包使用情况(转)
原文地址:axis2 webservice jar包使用情况 今天使用axis2webservice,整理了下jar包,方便以后时候. axis2 webservice 服务端jar包: --> ...
- org.apache.axis2.AxisFault: java.lang.Error: Unresolved compilation problem:
原创:转载请注明出处 今天遇到以下一个异常,找了好长时间,就是不知道什么原因, 在网上搜了好多,也没搜到相关的解决方法. 1.异常展示, org.apache.axis2.AxisFault: jav ...
- 2.1 Apache Axis2 快速学习手册之 POJO 构建Web Service
1. 准备:创建一个Maven Web App 项目 这里让我们使用Maven 模板创建一个Web App 项目 1. New------> Maven Project 2. 使用默认配置,点击 ...
- 译: 2. Apache Axis2安装指南
Apache Axis2安装指南 本文档提供有关Axis2分发包,系统先决条件以及设置环境变量和工具的信息,然后提供有关安装方法的详细说明. 请将您的反馈发送至:java-dev@axis.apach ...
- org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
原创:转载请注明出处 1.异常情况 org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected ...
- axis2 webservice 发布、调用与项目集成
发布 1.在apache官网下载axis2包,下载Binary Distribution和War Distribution两个zip. 2.将war放入tomcat webapps下部署.并输入 ht ...
随机推荐
- hdu 5288 OO’s Sequence(2015 Multi-University Training Contest 1)
OO's Sequence Time Limit: 4000/2000 MS (Jav ...
- linux man 1,2,3 命令
原文: http://blog.sina.com.cn/s/blog_969c52730101c0p7.html ------------------------------------------- ...
- nodejs连接sqlserver
nodejs连接sqlserver http://blog.csdn.net/kkkkkxiaofei/article/details/31353091
- 在GNS3中模拟交换机和PC
一.目标:使用ios c3640交换模块,模拟交换机 二.使用VPCS模拟PC机 VPCS模拟PC机软件下载: http://down.51cto.com/data/195524 步骤1.启动GN ...
- apache ab 測试 apr_socket_connect(): 因为目标机器积极拒绝 无法连接
遇到这样的情况通常是你开的并行数量太多了... 比如:ab -c 1000 -n 10000 http://localhost/index.html 如此大的请求就会挂掉,只是还是有补救措施的,能够通 ...
- javaWeb_使用标签库简化jsp
jsp标签库.也叫自己定义标签. 应用范围 jsp标签.主要应用于前台页面.在jsp中.假设jsp中存在<% %> 等 java代码.那么对前台开发者来说.就须要了解 java代码. 怎样 ...
- 32位win7系统下配置IIS遇到php-cgi.exe - FastCGI 进程意外退出问题的解决的方法
今天重装了一下系统,是32位的WIN7.装完系统后想把IIS装回来,由于有时候须要用到笔记本处理一些事情.结果WEBserver正常了.但IIS的FASTCGI模块始终不能解析PHP,一直报php-c ...
- Sublime Text 2 编辑器实用技巧大全
http://blog.163.com/lgh_2002/blog/static/440175262012429146486/
- ubuntu14.04 的ibus不能卸载(安装fcitx输入法框架时可能有这个需求)。出现无system setting有用程序
每年的ubuntu新版本号公布,都会吸引一大批热血青年. 关注越多也让ubuntu越来越好了. 使用ubuntu的人都会在安装系统之后马上安装顺手的输入法,也可能不会.看人. 安装输入法,对于中文输入 ...
- HDU 5073 数学题
题目传送门 http://acm.hdu.edu.cn/showproblem.php?pid=5073 这道题RE了好多发啊囧,RE到精神不振. Galaxy的质心并不是一成不变的,随着一些星球的移 ...