2013-04-24 110 views
0

我們有其他使用spring和cxf的web服務。當拋出一個檢查的異常時,我們通過soap ui獲得正確的本地化異常。其他Web服務異常返回堆棧跟蹤

但是,當一個未經檢查的運行時異常被拋出,我們只是得到一個堆棧跟蹤,指出web服務無法加載。

@XmlType 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement 
public class WebServiceExceptionWrapper extends Exception { 

    /** Type (class name). */ 
    private String type; 

    /** Message. */ 
    private String message; 

    /** Stack trace. */ 
    private StackTraceElement[] stackTrace; 

    /** Cause. */ 
    private Throwable cause; 

    /** The localized message. */ 
    private String localeErrorCode; 

    /** 
    * Construct a wrapper around an exception. 
    * 
    * @param e exception 
    */ 
    public WebServiceExceptionWrapper(final Exception e) { 
     this.type = e.getClass().getName(); 
     this.message = StringEscapeUtils.escapeHtml(e.getLocalizedMessage()); 
     this.stackTrace = e.getStackTrace(); 
     this.cause = e.getCause(); 
     if(e instanceof DetailedException) { 
      this.localeErrorCode = StringEscapeUtils.escapeHtml(((DetailedException) e).getLocaleErrorCode()); 
     } 
    } 

    /** 
    * Gets the type. 
    * 
    * @return the type 
    */ 
    @XmlElement(nillable = true) 
    public String getType() { 
     return this.type; 
    } 

    /** 
    * Sets the type. 
    * 
    * @param type the type to set 
    */ 
    public void setType(final String type) { 
     this.type = type; 
    } 

    /** 
    * Gets the message. 
    * 
    * @return the message 
    */ 
    @XmlElement(nillable = true) 
    public String getMessage() { 
     return this.message; 
    } 

    /** 
    * Sets the message. 
    * 
    * @param message the message to set 
    */ 
    public void setMessage(final String message) { 
     this.message = message; 
    } 

    /** 
    * Gets the stackTrace. 
    * 
    * @return the stackTrace 
    */ 
    @XmlElement(nillable = true) 
    public StackTraceElement[] getStackTrace() { 
     return this.stackTrace; 
    } 

    /** 
    * Sets the stackTrace. 
    * 
    * @param stackTrace the stackTrace to set 
    */ 
    public void setStackTrace(final StackTraceElement[] stackTrace) { 
     this.stackTrace = stackTrace; 
    } 

    /** 
    * Gets the cause. 
    * 
    * @return the cause 
    */ 
    @XmlElement(nillable = true) 
    public Throwable getCause() { 
     return this.cause; 
    } 

    /** 
    * Sets the cause. 
    * 
    * @param cause the cause to set 
    */ 
    public void setCause(final Throwable cause) { 
     this.cause = cause; 
    } 

    /** 
    * @return the localeErrorCode 
    */ 
    @XmlElement(nillable = true) 
    public String getLocaleErrorCode() { 
     return this.localeErrorCode; 
    } 

    /** 
    * @param localeErrorCode the localeErrorCode to set 
    */ 
    public void setLocaleErrorCode(final String localeErrorCode) { 
     this.localeErrorCode = localeErrorCode; 
    } 

我該怎麼做,以防止返回堆棧跟蹤。我只想要一個響應代碼和一個響應消息。

回答

1

使用Apache-CXF處理JAX-RS webservices中異常流程的最簡單方法是通過javax.ws.rs.ext.ExceptionMapper類標記@Provider註釋(或者在您的jaxrs:server的Provider部分中) (JAXRS:供應商),你可以在你想返回特定狀態代碼實例,然後捕獲特定的例外:

@Provider 
public class FooExceptionMapper implements ExceptionMapper<FooException> { 

    @Override 
    public Response toResponse(FooException exception) { 
     ... do your work you want to maybe do here 
     return Response.status(Response.Status.BAD_REQUEST).entity(...whatever...).build(); 
    } 
} 

現在你創建這個映射一個bean(或使用註解驅動),把它綁您服務方法,然後捕獲你的異常。在你的catch塊中,簡單地拋出這個,讓cxf的攔截器框架接管。

+0

但是我們如何處理w ith錯誤? – 2014-08-30 22:08:19

+0

你是什麼意思我們如何處理錯誤?該框架將通過將其轉換爲500來處理任何您未明確匹配的內容,這可能是最正確的行爲。 – fpmoles 2014-11-13 19:27:09