使用JSON的Spring 3exception处理
我有一个Controller
,我想向用户反馈出错的地方。 执行错误回调但错误消息未发送回客户端。
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 validateSteps( @RequestBody List<Map> testCaseInputs, HttpServletResponse response) throws MalformedMessageException, MalformedProfileException, XmlException, IOException, MissingDependencyException, MessageValidationException { List validations = new ArrayList(); ... return validations; }
控制器中的exception处理程序:
@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
参数undefined
。
看看我对一个非常相似的问题的回答: Spring MVC返回JSONS和exception处理