2012-11-06 70 views
0

我定義爲一個Spring控制器以下異常處理程序:如何從用戶端的Spring response.sendError(...)檢索描述性消息?

@ExceptionHandler(Customized4ExceptionHandler.class) 
public void handleCustomized4Exception(
    Customized4ExceptionHandler ex, 
    HttpServletRequest request, 
    HttpServletResponse response) throws IOException { 

     response.sendError(HttpStatus.EXPECTATION_FAILED.value(), 
     "zzzzzzz"); 

} 

當觸發錯誤,我得到用戶側以下幾點:

enter image description here

錯誤代碼是正確,但不顯示「zzzzzzz」描述性消息。我如何在用戶端顯示它?

我的JavaScript:

$.ajax({ 
    type: 'GET', 
    url: prefix + "/throwCustomized4ExceptionHandler", 
    dataType: 'json', 
    async: true, 
    success: function(result) { 
     alert('Unexpected success !!!'); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     alert(jqXHR.status + " " 
        + textStatus + " " 
       + errorThrown + " !"); 
    } 
}); 

回答

0

我解決了這個問題如下:

@ExceptionHandler(Customized4ExceptionHandler.class) 
@ResponseStatus(value=HttpStatus.BAD_REQUEST) 
@ResponseBody 
public String handleCustomized4Exception(
    Customized4ExceptionHandler ex) { 

    // return "zzzzzzz" 
    return ex.getSpecialMsg(); 

} 

$.ajax({ 
    type: 'GET', 
    url: prefix + "/throwCustomized4ExceptionHandler", 

    async: true, 
    success: function(result) { 
     alert('Unexpected success !!!'); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     alert(jqXHR.status + " " + jqXHR.responseText); 
    } 
}); 
1

它應該作爲jqXHR.statusText

+0

我剛試過,但我還是不明白 「ZZZZZ」。 – JVerstry

相關問題