使用 JSON 的 Spring 3 异常处理

发布于 2024-10-31 14:08:30 字数 2095 浏览 0 评论 0原文

我有一个控制器,我想向用户反馈出现的问题。错误回调被执行,但错误消息没有发送回客户端。

JQuery 调用:

var validateButton = $('#validateSteps');
validateButton.bind('click', function() {
    var stepsInput = $(':input').serializeArray();
    $.postJSON('validate.htm', stepsInput, function(data) {
        alert(data);
        var steps = $('#steps');
        var i = 0;
        for(i=0;i<data.length;i++) {
            steps.stepWidget('setValidationStatus', {testStepId: data[i].testStepId, interactionType: data[i].interactionType, validationStatus: data[i].validationStatus} );
            steps.stepWidget('setErrorDescriptions', {testStepId: data[i].testStepId, interactionType: data[i].interactionType, errorDescriptions: data[i].errorDescriptions} );
        }
        return false;
    }, {
            error: function (XMLHttpRequest, textStatus, errorThrown, data) {
                alert("error function");
                alert(textStatus);
                alert(errorThrown);               
                alert("Internal Server Error: " + data);
            return false;
        }
    });
    return false;
});

控制器:

@RequestMapping(value = "validate.htm", method = RequestMethod.POST)
public @ResponseBody
List<ValidationBean> validateSteps(
        @RequestBody List<Map<String, String>> testCaseInputs,
        HttpServletResponse response) throws MalformedMessageException,
        MalformedProfileException, XmlException, IOException,
        MissingDependencyException, MessageValidationException {
    List<ValidationBean> validations = new ArrayList<ValidationBean>();
    ...
    return validations;
}

控制器中的异常处理程序:

@ExceptionHandler(Exception.class)
public @ResponseBody
String handleException(Exception e, HttpServletResponse response) {
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return e.getMessage();
}

我想向用户显示的是 handleException 方法应返回的 String。在 error 回调中,data 参数是未定义

I have a Controller and I want to get feedback to the user of what went wrong. The error callback is executed but the error message is not sent back to the client.

The JQuery call:

var validateButton = $('#validateSteps');
validateButton.bind('click', function() {
    var stepsInput = $(':input').serializeArray();
    $.postJSON('validate.htm', stepsInput, function(data) {
        alert(data);
        var steps = $('#steps');
        var i = 0;
        for(i=0;i<data.length;i++) {
            steps.stepWidget('setValidationStatus', {testStepId: data[i].testStepId, interactionType: data[i].interactionType, validationStatus: data[i].validationStatus} );
            steps.stepWidget('setErrorDescriptions', {testStepId: data[i].testStepId, interactionType: data[i].interactionType, errorDescriptions: data[i].errorDescriptions} );
        }
        return false;
    }, {
            error: function (XMLHttpRequest, textStatus, errorThrown, data) {
                alert("error function");
                alert(textStatus);
                alert(errorThrown);               
                alert("Internal Server Error: " + data);
            return false;
        }
    });
    return false;
});

The Controller:

@RequestMapping(value = "validate.htm", method = RequestMethod.POST)
public @ResponseBody
List<ValidationBean> validateSteps(
        @RequestBody List<Map<String, String>> testCaseInputs,
        HttpServletResponse response) throws MalformedMessageException,
        MalformedProfileException, XmlException, IOException,
        MissingDependencyException, MessageValidationException {
    List<ValidationBean> validations = new ArrayList<ValidationBean>();
    ...
    return validations;
}

The Exception Handler in the Controller:

@ExceptionHandler(Exception.class)
public @ResponseBody
String handleException(Exception e, HttpServletResponse response) {
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return e.getMessage();
}

What I want to show to the user is the String that should be returned by the handleException method. In the error callback the data parameter is undefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文