zoukankan      html  css  js  c++  java
  • springboot自定义拦截器和全局异常处理器

    一、自定义拦截器

    @Configuration//声明这是一个配置
    public class MyInterceptor extends WebMvcConfigurerAdapter {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            HandlerInterceptor inter = new HandlerInterceptor() {
    
                @Override
                public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
                    System.out.println("自定义拦截器......");
                    return true;
                }
    
                @Override
                public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
                        throws Exception {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
                        throws Exception {
                    // TODO Auto-generated method stub
    
                }
            };
            registry.addInterceptor(inter).addPathPatterns("/**");
        }
    }

    二、全局异常处理器

    //全局异常处理器
    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public Map<String, Object> handleException(Exception exception) {
            Map<String, Object> map = new HashMap<>();
            map.put("errorCode", 500);
            map.put("errorMsg", exception.toString());
            return map;
        }
    }
  • 相关阅读:
    wsl安装torch-0.4.0 cpu版本
    基于TimeLine编辑角色动画(三)
    unity在Game窗口绘制网格Capsule
    unityGame窗口绘制Box
    unity在Game窗口绘制网格球
    读取Excal数据通过反射赋值
    根据Excal表生成代码
    状态模式设计动画状态机
    第三人称相机
    Nhibernate配置MySQL踩坑记录
  • 原文地址:https://www.cnblogs.com/linding/p/12591880.html
Copyright © 2011-2022 走看看