/** * <> * <错误拦截 返回json给调用方> * * @Author jason_w * @Date 2019/1/9 0009 21:27 * @see */ @ControllerAdvice public class ExceptionHandlerAdvice { private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlerAdvice.class); /** *<异常返回给前段JSON数据,前端根据错误处理> *@param * *@return *@Author jason_wang */ @ExceptionHandler(value = Exception.class) @ResponseBody public String exception(Exception ex, WebRequest request)throws Exception{ LOGGER.error("ExceptionHandlerAdvice拦截到异常,e:{}",ex.getMessage()); Result pageList=new Result(); pageList.setCode(1); if (ex instanceof UnauthorizedException){ pageList.setMsg("您没有权限进行此操作"); }else if (ex instanceof AppException){ pageList.setMsg(ex.getMessage()); }else { pageList.setMsg("未知异常"); } return new ObjectMapper().writeValueAsString(pageList); } @ModelAttribute public void AddAttribute(Model model){ model.addAttribute("user", SubjectUtil.getUser()); } }
@ControllerAdvice拦截异常返回数据
最新推荐文章于 2024-08-06 08:45:00 发布