美文网首页
Spring-boot异常拦截

Spring-boot异常拦截

作者: liangxifeng833 | 来源:发表于2019-04-14 17:52 被阅读0次

    一.拦截步骤

    • 请求页面不存在的时候 拦截 Whitelabel Error Page, 该页面是Spring-boot默认的页面.
      image.png
    • 在application.yml中如下配置:
      #禁用spring boot 的异常页面 <<Whitelabel Error Page>>
        error:
          whitelabel:
            enabled: true
      
    • 自己实现ErrorController接口.
        package com.soufang.esproject.base;
      
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.boot.autoconfigure.web.ErrorAttributes;
        import org.springframework.boot.autoconfigure.web.ErrorController;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.ResponseBody;
        import org.springframework.web.context.request.RequestAttributes;
        import org.springframework.web.context.request.ServletRequestAttributes;
      
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.util.Map;
      
        /**
         * web错误 全局配置
         * Created by liangxifeng.
         */
        @Controller
        public class AppErrorController implements ErrorController {
            private static final String ERROR_PATH = "/error";
      
            private ErrorAttributes errorAttributes;
      
            @Override
            public String getErrorPath() {
            return ERROR_PATH;
            }
      
            @Autowired
            public AppErrorController(ErrorAttributes errorAttributes) {
            this.errorAttributes = errorAttributes;
            }
      
            /**
             * Web页面错误处理
             * 客户端使用 浏览器 方式请求异常时会触发
             */
            @RequestMapping(value = ERROR_PATH, produces = "text/html")
            public String errorPageHandler(HttpServletRequest request, HttpServletResponse response) {
            int status = response.getStatus();
            //访问resources/templates/403.html,404.html,500.html
            switch (status) {
                case 403:
                    return "403";
                case 404:
                    return "404";
                case 500:
                    return "500";
            }
      
            return "index";
            }
      
            /**
             * 除Web页面外的错误处理,比如Json/XML等
             * 客户端使用 rest 方式请求异常时会触发
             */
            @RequestMapping(value = ERROR_PATH)
            @ResponseBody
            public ApiResponse errorApiHandler(HttpServletRequest request) {
            RequestAttributes requestAttributes = new ServletRequestAttributes(request);
      
            Map<String, Object> attr = this.errorAttributes.getErrorAttributes(requestAttributes, false);
            int status = getStatus(request);
      
            return ApiResponse.ofMessage(status, String.valueOf(attr.getOrDefault("message", "error")));
            }
      
            private int getStatus(HttpServletRequest request) {
            Integer status = (Integer) request.getAttribute("javax.servlet.error.status_code");
            if (status != null) {
                return status;
            }
            return 500;
            }
        }
      

    二. ApiResonse内容请查看

    相关文章

      网友评论

          本文标题:Spring-boot异常拦截

          本文链接:https://www.haomeiwen.com/subject/hjyswqtx.html