【问题标题】:How to add stacktrace into soap fault (websphere)如何将堆栈跟踪添加到肥皂故障(websphere)中
【发布时间】:2012-08-17 15:34:45
【问题描述】:


我们已将 Webservice 部署到 websphere 服务器中,我想询问有关控制肥皂故障响应的设置。当肥皂故障发生时,我想将堆栈跟踪日志添加到回复中。我知道这在 weblogic 中是可能的。如何在 websphere 中实现这一点?或者有没有办法手动添加它(不是通过创建自定义元素)?

谢谢

编辑:我使用 apache cxf 生成基本 java 类,所以我有:

@WebMethod public returnType method(param) throws CustomException

并制造故障

CustomExceptionWSDLType ex = new CustomExceptionWSDLType ()
throw new CustomException(error.getMessage(), ex , error);

CustomException 是异常 和
CustomExceptionWSDLType 是复杂类型 (均由 cxf 生成)

编辑2: 我使用 CXF 生成 POJO,但 Websphere 使用自己的轴实现来部署我的 WS。

【问题讨论】:

  • 我找到了stackoverflow.com/a/3437062/571816,但axis2.xml在哪里?应该在我耳边吗?
  • 您声明您正在使用 CXF。 Axis2 是一个不同的 WS 堆栈。你的耳朵里不应该有axis2.xml,因为你没有使用axis2。
  • 我只使用 cxf 来生成 POJO。 Websphere 使用axis2 框架部署@Webservice,所以我需要专注于axis。无论如何,我在 plugins\org.apache.axis2.jar 中找到了axis2.xml,但设置 sendStacktraceDetailsWithFaults 没有效果
  • 啊,我想我现在明白了。您正在使用 wsdl2java 生成 POJO,对吗?我强烈建议改用 wsimport 。这个工具是 JAX-WS RI 的一部分(它在 JDK 中)。有关更多上下文,请参阅此答案:stackoverflow.com/a/3590252/1127892

标签: java soap websphere axis2 stack-trace


【解决方案1】:

我不是 Websphere 专家,无法告诉您是否有配置选项可以让您执行此操作。

或者有没有办法手动添加它(不是通过创建自定义元素)?

当您抛出故障时,您始终可以在 Web 服务中添加详细信息并修改故障字符串和代码。现在,有很多方法可以构造和抛出错误,我不知道您的 Web 服务是如何做到的。这是一个非常简单的示例,它将异常的堆栈跟踪放入故障字符串中。

   @WebMethod
public void throwFault(){
    try {
        SOAPFactory factory = SOAPFactory.newInstance();            
        IndexOutOfBoundsException e = new IndexOutOfBoundsException("index out of bounds");         
        SOAPFault fault = factory.createFault(getStackTraceString(e), new QName("http://whatever.com","CustomFault"));          
        throw new SOAPFaultException(fault);
    } catch (SOAPException e) {
        // ignore for the example           
    }
}

private String getStackTraceString(Exception e){
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    return sw.toString();
}

方法throwFault 被服务公开并且简单地创建并抛出一个新的SOAPFault。这在您的代码中可能看起来不同。私有方法getStackTraceString 将堆栈跟踪转换为字符串表示。

此解决方案确实向您的 WSDL 添加了一个附加元素,它只是将错误字符串重用于堆栈跟踪。

调用网络服务,我得到以下响应:

 <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
 <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
  <faultcode xmlns:ns0="http://whatever.com">ns0:CustomFault</faultcode> 
  <faultstring>java.lang.IndexOutOfBoundsException: index out of bounds at Faulter.throwUndeclaredFault(Faulter.java:23) at  <!--rest of stacktrace omitted for readability--!> </faultstring> 
 </S:Fault>
  </S:Body>
  </S:Envelope>

编辑: 假设你代码中的变量error是一个异常,你可以把你的throw语句改成

throw new CustomException(getStackTraceString(error),error);

这应该以上述方式为您提供堆栈跟踪。

【讨论】:

  • 可能我应该提到我使用 apache CXF 从 WSDL 生成 Java 类。
  • 没关系。如果在实现从 WSDL 生成的类的 CXF 服务中有此代码。该服务也使用此 WSDL 进行部署。
  • 是的,我可以将堆栈跟踪放入错误消息中,但这并不是我想要的。
  • 你想把堆栈跟踪放在哪里?如果不在故障字符串中,那么您只有故障代码或故障的详细子元素。
  • 好的,我自己将它添加到自定义元素中,螺丝轴:) thx 4 help
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 2017-12-31
相关资源
最近更新 更多