2013-03-12 44 views
9

我正在使用JAX-WS創建Web服務(我正在使用Java to WSDL方法創建此服務)。沒有出現在WSDL中的JAX-WS Soap故障

我無法讓我的異常工作,因爲我需要。

我創建了以下異常類:

@WebFault 
public class MyWebServiceException extends SOAPFaultException { 

    private static final long serialVersionUID = 8234753208722614057L; 

    protected MyWebServiceException(SOAPFault fault) { 
     super(fault); 
    } 

    protected MyWebServiceException(SOAPFault fault, Throwable throwable) { 
     this(fault); 
     super.setStackTrace(throwable.getStackTrace()); 
    } 
} 

這讓我在我的SOAP響應如下:

<faultcode>E0010</faultcode> 
<faultstring>Invalid Report</faultstring> 
<detail> 
    <ns2:exception class="com.example.web.common.exceptions.MyWebServiceException" note="To disable this feature, set com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false" xmlns:ns2="http://jax-ws.dev.java.net/"> 
    <message>Invalid Report</message> 
    <ns2:stackTrace> 
    <ns2:frame class="com.example.web.common.exceptions.ExceptionHandler" file="ExceptionHandler.java" line="34" method="getWebException"/> 

但是,因爲我的例外是SOAPFaultException延伸RuntimeException,它不會被添加到WSDL中,因此當服務的用戶生成其客戶端存根時,不包括該異常。

有誰知道如何創建此異常給我想要的輸出(即包括在faultcode和faultstring),但也出現在WSDL(我猜它必須是經過檢查的異常)我搜索SO

高低來拿出我已經有的東西!

+0

什麼是JAX-WS運行時? – 2013-03-13 19:40:29

+0

您使用哪種JAX-WS實現來構建? – Nick 2013-03-18 18:03:04

+0

'wsimport -version JDK 6中的JAX-WS RI 2.1.6' – cowls 2013-03-19 08:32:31

回答

4

如果聲明WebMethod拋出MyWebServiceException,它將出現在WSDL上。

但可能你不應該搞亂SOAPFault代碼。如果你創建一個業務異常並拋出它,你可以在客戶端得到錯誤代碼,並與肥皂內部進行混淆。

你的WebMethod會是什麼樣子:

@WebMethod 
public String sayHello(@WebParam String name, @WebParam String thing) throws MyException 
{ 
    // TODO Auto-generated method stub 
    if ("err".equals(name)) 
     throw new MyException("E0010","Report not found"); 
    return null; 
} 

您的業務異常:

public class MyException extends Exception { 

    private static final long serialVersionUID = -923394585528818428L; 
    public MyException(String errorCode,String errorMessage) 
    { 
     this.errorCode = errorCode; 
     this.errorMessage = errorMessage; 
    } 

    public String getErrorCode() { 
     return errorCode; 
    } 
    public void setErrorCode(String errorCode) { 
     this.errorCode = errorCode; 
    } 
    public String getErrorMessage() { 
     return errorMessage; 
    } 
    public void setErrorMessage(String errorMessage) { 
     this.errorMessage = errorMessage; 
    } 
    String errorCode; 
    String errorMessage; 

} 

而且回onException的將是:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
     <soap:Fault> 
     <faultcode>soap:Server</faultcode> 
     <faultstring>Fault occurred while processing.</faultstring> 
     <detail> 
      <ns1:MyException xmlns:ns1="http://ws/"> 
       <errorMessage xmlns:ns2="http://ws/">Report not found</errorMessage> 
       <errorCode xmlns:ns2="http://ws/">E0010</errorCode> 
      </ns1:MyException> 
     </detail> 
     </soap:Fault> 
    </soap:Body> 
</soap:Envelope> 

也可以加一個堆棧跟蹤,但那會更好地登錄服務器,也許用GUUID來關聯客戶端錯誤i f需要。

+0

最後一起去了這個。這項工作,刪除堆棧跟蹤和響應是相當乾淨的 – cowls 2013-03-28 18:08:35

1

假設您正在使用JAX-WS而不是JAX-RPC,則可以刪除extends SOAPFaultException。然後添加必要的字段來自己的異常:

這是一個StackTraceElement的(就像一個例子):

public class SOAPStackElement implements Serializable { 
     private static final long serialVersionUID = 1L; 

     @XmlAttribute(name="class") 
     private String class; 
     @XmlAttribute(name="file") 
     private String file; 
     @XmlAttribute(name="methodname") 
     private String methodName; 
     @XmlAttribute(name="line") 
     private Integer line; 
    } 

這是SOAP的信息:

public class SOAPFaultInfo implements Serializable { 
     @XmlElementWrapper(name="stackTrace") 
     private SOAPStackElement[] stackTrace; 
     @XmlElement(name="faultcode") 
     private String faultCode; 
     @XmlElement(name="faultstring") 
     private String faultString; 
     /* what else your heart desires. :-) */ 
    } 

這是真正的例外:

@WebFault 
    public class MyWebServiceException extends Exception { 

     private static final long serialVersionUID = 1L; 
     private SOAPFaultInfo faultInfo; 

     protected MyWebServiceException(SOAPFaultInfo fault) { 
      super(); 
      this.faultInfo = fault; 
     } 

     protected MyWebServiceException(SOAPFaultInfo fault, Throwable cause) { 
      super(cause); 
      this.faultInfo = fault; 
     } 

     public SOAPFaultInfo getFaultInfo() { 
      return faultInfo; 
     } 
    } 

我真的沒有得到解決這個到測試項目。所以你可能需要解決一些編譯問題。但我希望你能得到這張照片。

+1

我不太關心堆棧跟蹤,更關心'faultCode'和'faultString'元素,你能展示一下你將如何在你的例子中集成這些元素嗎? – cowls 2013-03-25 10:58:33

+0

我將這兩個元素添加到'SOAPFaultInfo'類中。 – secra 2013-03-25 11:09:57

+0

我還沒有實際測試過這個,但它看起來是最有希望的答案,今天晚些時候會試一試,並讓你知道它是如何去的。 – cowls 2013-03-26 08:31:19