设置全局异常,将异常信息指定内容展示给前端页面,保证程序的安全性
@Slf4j@Componentpublic class ExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { log.error("{} Exception",httpServletRequest.getRequestURI(),e); ModelAndView modelAndView = new ModelAndView(new MappingJacksonJsonView()); //当使用Jackson 2.x.时,使用对应的MappingJackson2JsonView, 这里使用的是1.9 modelAndView.addObject("status",ResponseCode.ERROR.getCode()); modelAndView.addObject("msg","接口异常,详情请查看服务端日志的异常信息"); modelAndView.addObject("data", e.toString()); return modelAndView; }}
原文地址:https://www.cnblogs.com/FondWang/p/11711492.html
时间: 2024-11-10 08:06:00