Spring-异常解析器

来源:互联网 发布:mac php环境 编辑:程序博客网 时间:2024/06/03 15:50

本文描述了当系统内部错误时,如何把JSON对象返回给前台

1.pom文件引用fastJson2.xml文件中配置<bean id="exceptionHandler" class="xxx.xxx包.er" />  3.JAVA代码,必须且只要实现HandlerExceptionResolver类import com.alibaba.fastjson.support.spring.FastJsonJsonView;public class ExceptionResolver implements HandlerExceptionResolver {    public static final long serialVersionUID = 1L;    @Override    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,            Exception ex) {        ModelAndView mv = new ModelAndView();        FastJsonJsonView view = new FastJsonJsonView();        Map<String, Object> attributes = new HashMap<String, Object>();        attributes.put("msg", "随便写点什么");        attributes.put("resultCode", "系统错误");        view.setAttributesMap(attributes);        mv.setView(view);        return mv;    }}