Java 如何从用户端的Spring response.sendError(…)检索描述性消息?
我在Spring控制器中定义了一个异常处理程序,如下所示:Java 如何从用户端的Spring response.sendError(…)检索描述性消息?,java,spring,jquery,exception-handling,Java,Spring,Jquery,Exception Handling,我在Spring控制器中定义了一个异常处理程序,如下所示: @ExceptionHandler(Customized4ExceptionHandler.class) public void handleCustomized4Exception( Customized4ExceptionHandler ex, HttpServletRequest request, HttpServletResponse response) throws IOException {
@ExceptionHandler(Customized4ExceptionHandler.class)
public void handleCustomized4Exception(
Customized4ExceptionHandler ex,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.sendError(HttpStatus.EXPECTATION_FAILED.value(),
"zzzzzzz");
}
触发错误时,我在用户端得到以下信息:
错误代码正确,但不显示“zzzzz”说明性消息。如何在用户端显示它
我的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 + " !");
}
});
它应该以
jqXHR.statusText
的形式提供。我解决了以下问题:
@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);
}
});