SpringMVC 拦截异常Exception返回Json,适合Java做后台业务项目

public class ExceptionHandler implements HandlerExceptionResolver {

@Override

public ModelAndView resolveException(HttpServletRequest request,

HttpServletResponse response, Object handler, Exception ex) {

Logger.getLogger(ex.getClass()).error(“异常”, ex);

// 输出错误Json

ModelAndView mav = new ModelAndView();

MappingJackson2JsonView view = new MappingJackson2JsonView();

Map<String, Object> result = new HashMap<String, Object>();

result.put(“Code”, -1);

result.put(“Message”, “处理异常,请稍后再试”);

result.put(“Data”, “”);

view.setAttributesMap(result);

mav.setView(view);

return mav;

}

}

    原文作者:jsyhello
    原文地址: https://blog.csdn.net/jsyhello/article/details/49506537
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞