Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从用户端的Spring response.sendError(…)检索描述性消息?_Java_Spring_Jquery_Exception Handling - Fatal编程技术网

Java 如何从用户端的Spring response.sendError(…)检索描述性消息?

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 {

我在Spring控制器中定义了一个异常处理程序,如下所示:

@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);
    }
});