package com.qmtt.config; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import com.qmtt.common.BizException; import com.qmtt.common.JsonRet; /** * @author admin 2017年4月6日 全局异常处理 */ @ControllerAdvice public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); // public static final String DEFAULT_ERROR_VIEW = "error"; // // /** // * 返回错误页面 // * // * @param req // * @param e // * @return // * @throws Exception // */ // @ExceptionHandler(value = Exception.class) // public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception // e) throws Exception { // ModelAndView mav = new ModelAndView(); // mav.addObject("exception", e); // mav.addObject("url", req.getRequestURL()); // mav.setViewName(DEFAULT_ERROR_VIEW); // return mav; // } /** * 处理异常,返回 * * @param req * @param e * @return * @throws Exception */ @ExceptionHandler(value = BizException.class) @ResponseBody public Object jsonErrorHandler(HttpServletRequest req, BizException e) throws Exception { log.error("", e); JsonRet ret = new JsonRet(); ret.setBizException(e); return ret; } @ExceptionHandler(value = Exception.class) @ResponseBody public Object jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception { log.error("", e); JsonRet ret = new JsonRet(); ret.setException(e); return ret; } }