/** * <> * < Error interception return json To the caller > * * @Author jason_w * @Date 2019/1/9 0009 21:27 *
@see */ @ControllerAdvice public class ExceptionHandlerAdvice { private static
final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
/** *< Exception returns to the previous paragraph JSON data , Error handling based on front end > *@param * *@return *@Author jason_wang */
@ExceptionHandler(value = Exception.class) @ResponseBody public String
exception(Exception ex, WebRequest request)throws Exception{
LOGGER.error("ExceptionHandlerAdvice Intercept exception ,e:{}",ex.getMessage()); Result
pageList=new Result(); pageList.setCode(1); if (ex instanceof
UnauthorizedException){ pageList.setMsg(" You do not have permission to do this "); }else if (ex instanceof
AppException){ pageList.setMsg(ex.getMessage()); }else {
pageList.setMsg(" Unknown exception "); } return new
ObjectMapper().writeValueAsString(pageList); } @ModelAttribute public void
AddAttribute(Model model){ model.addAttribute("user", SubjectUtil.getUser()); }
}
Technology