抛出一个 Error resolving template template might not exist or might not be accessible 的异常
在ajax的异常处理中,直接返回IMoocJSONResult是不行的,会再抛出一个
Error resolving template template might not exist or might not be accessible 的异常,而不是我们预想的byZero的异常,这时候需要使用MappingJackson2HttpMessageConverter把IMoocJSONResult手动写入 HttpServletResponse中。
private static final String ERROR_VIEW = "error";
@Autowired
private MappingJackson2HttpMessageConverter jsonConverter;
@ExceptionHandler(value = Exception.class)
public Object error(HttpServletRequest request, HttpServletResponse response, Exception e) throws HttpMessageNotWritableException, IOException {
if(isAjax(request)) {
MyJSONResult result = MyJSONResult.errorException(e.getMessage());
jsonConverter.write(result, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
return null;
} else {
ModelAndView mav = new ModelAndView();
mav.addObject("exception",e);
mav.addObject("url", request.getRequestURL());
mav.setViewName(ERROR_VIEW);
return mav;
}
}