• spring boot统一异常页面


    只需要创建一个类就可以了

    package com.ulic.gis.securityManage.controller;
    
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.error.ErrorAttributes;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.context.request.ServletWebRequest;
    import org.springframework.web.servlet.ModelAndView;
    
    @ControllerAdvice
    public class GlobalExceptionHandler {
        private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
        @Autowired
        private ErrorAttributes errorAttributes;
        /**
         * 出现异常之后会跳转到此方法
         * @param request
         * @param e
         * @return
         */
        @ExceptionHandler(Exception.class) 
        public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
            ModelAndView mav = new ModelAndView("/exception/blank"); 
            Map<String, Object> map = getErrorAttributes(request, false);
            log.info("错误码status:{}",map.get("status"));
            //log.info("失败的请求:{}",map.get("URL"));
            mav.addObject("message","请求失败,存在异常数据");
            return mav;
        }
         
        private Map<String, Object> getErrorAttributes(HttpServletRequest request,
                boolean includeStackTrace) {
    
            ServletWebRequest servletWebRequest = new ServletWebRequest(request);
            Map<String, Object> map = this.errorAttributes.getErrorAttributes(servletWebRequest,includeStackTrace);
            String URL = request.getRequestURL().toString();
            map.put("URL", URL); 
            return  map;
        }
    }
  • 相关阅读:
    restore db fail
    MAC 设置环境变量path的常用方法
    python编码问题 decode与encode
    python 正则表达式 提取网页中标签的中文
    转:Linux实时将所有输出重定向到文件
    selenium 定位无标签的元素
    re.compile
    Python urllib.quote
    Shell #*/ 和 %/*
    [转载] python必碰到的问题---encode与decode,中文乱码
  • 原文地址:https://www.cnblogs.com/yinyl/p/10830131.html
Copyright © 2020-2023  润新知