使用 JSON 的 Spring 3 异常处理
我有一个控制器,我想向用户反馈出现的问题。错误回调被执行,但错误消息没有发送回客户端。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论