springMVC全局异常配置
通常开发项目时如果代码有错的话,可能会在页面中显示出详细的异常错误信息并且会定位到自己的项目代码中详细的第几行出错可能也会在展示页面中出现,这样子不利于代码的安全性。
所以常常使用实现接口HandlerExceptionResolver的方法:
/** * @author 大神爱吃茶 * @Date 2020/1/10 0010 下午 16:53 */ @Component public class ExceptionResolver implements HandlerExceptionResolver { private static Logger logger = LoggerFactory.getLogger(ExceptionResolver.class); @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //说明此URI发生了异常,并把异常通过ex打印到控制台上面 logger.error("{} Exception",request.getRequestURI(), ex); ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView()); modelAndView.addObject("status", ResponseCode.ERROR.getCode()); modelAndView.addObject("msg", "接口异常,详情请查看服务日志"); modelAndView.addObject("data", ex.toString()); return modelAndView; } }
效果:
相关推荐
dlutbob 2020-07-04
zhangdy0 2020-05-31
csuzxm000 2020-06-09
84560296 2020-05-30
HappyHeng 2020-05-28
haidaoxianzi 2020-02-16
MicroBoy 2020-01-17
xxuncle 2020-01-13
咻pur慢 2020-01-10
melonjj 2019-12-26
luojinbai 2019-12-17
方志朋 2019-12-14
JudeJoo 2019-11-18
neweastsun 2019-11-07
Julywhj 2019-11-07
Julywhj 2019-11-02
yusongwhu 2019-10-22
neweastsun 2014-09-16
周太郎 2015-02-02