简体   繁体   English

如何从axis2故障响应中排除stacktrace

[英]How to exclude stacktrace from axis2 fault response

I have an Axis2 web service which throws different detail messages in the fault response to signal problems in the call. 我有一个Axis2 Web服务,该服务在故障响应中引发不同的详细消息,以解决呼叫中的信号问题。

At some point, due to server errors (others than the ones treated by the web service), in the fault detail string I get the full stacktrace of what happened. 在某些时候,由于服务器错误(Web服务未处理的错误),在故障详细信息字符串中,我获得了所发生情况的完整堆栈跟踪。 I do not want the client to see the stack trace, so (as a catch all errors) I want to output a simple "Server error" message with no stacktrace, no nothing. 我不希望客户端看到堆栈跟踪,因此(作为捕获所有错误),我想输出一个简单的“服务器错误”消息,其中没有stacktrace,什么也没有。

What is the simplest way of intercepting fault responses and changing the fault message. 拦截故障响应和更改故障消息的最简单方法是什么。 Are modules the only way of (complicated) doing this? 模块是这样做(复杂)的唯一方法吗?

Or, is there a configuration in Axis2 that says not to display stacktrace in fault? 还是Axis2中有一个配置,该配置说不显示故障堆栈跟踪?

Thanks! 谢谢!

I once had a similar problem. 我曾经有过类似的问题。 Not sure if there is some config to turn off the stacktrace showing, at least none that I could find at that moment (that would have been the best solution). 不知道是否有一些配置可以关闭显示的stacktrace,至少我当时没有找到(这将是最好的解决方案)。 Instead, I opted for a quick and dirty approach, mostly due to lack of time. 相反,我选择了一种快速而肮脏的方法,主要是因为时间不够。

What I did was to provide Axis2 with the detail of the fault myself. 我所做的就是自己为Axis2提供故障的详细信息。 The Axis2 servlet has a method called handleFault which deals with generating the fault. Axis2 Servlet具有一个称为handleFault的方法,该方法处理生成故障。 More exactly (deeper in the call) the MessageContextBuilder.createFaultEnvelope method is used to construct the fault element. 更准确地(在调用中更深), MessageContextBuilder.createFaultEnvelope方法用于构造故障元素。

Having the stacktrace in the detail is the default behavior, but there are ways to specify your custom detail. 在详细信息中包含stacktrace是默认行为,但是有一些方法可以指定您的自定义详细信息。 One way is to use the the AxisFault 's detail field in which you can add an OMElement (refer to AXIOM ) to be placed into the fault. 一种方法是使用AxisFaultdetail字段,您可以在其中添加OMElement (请参阅AXIOM )以放入故障中。 So you do something like: 因此,您可以执行以下操作:

public class MyServlet extends AxisServlet {
  ...
  public void handleFault(MessageContext msgContext, OutputStream out, AxisFault e) {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMElement detail = factory.createElement(...);
    e.setDetail(detail);
    // now let axis do its thing with the new improved AxisFault
    super.handleFault(msgContext, out, e);
  }
}

Now, instead of the exception stacktrace, your detail will be added instead. 现在,代替异常堆栈跟踪,将添加您的详细信息。

Axis2 uses Apache commons logging and the AxisFault messages that you are seeing are generated by code in Axis2 that looks similar to: Axis2使用Apache Commons日志记录,您看到的AxisFault消息是由Axis2中的代码生成的,类似于以下代码:

 try { executeMethod(httpClient, msgContext, url, getMethod); handleResponse(msgContext, getMethod); } catch (IOException e) { log.info("Unable to sendViaGet to url[" + url + "]", e); throw AxisFault.makeFault(e); } finally { cleanup(msgContext, getMethod); } 

[This code segment comes from org.apache.axis2.transport.http.HTTPSender] [此代码段来自org.apache.axis2.transport.http.HTTPSender]

So refer to apache commons logging user guide for instructions on how to set the logging levels and destination of the messages. 因此,请参阅《 Apache Commons Logging用户指南》,以获取有关如何设置消息的日志记录级别和目标的说明。

Hope this helps. 希望这可以帮助。

Can you not just catch the AxisFault 你能赶上AxisFault

try {
    // do stuff
} catch (AxisFault f) {
    log.error("Encountered error doing stuff", f);
    throw new IOException("Server error");
}

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

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