Fork me on Gitee

Spring Boot 异常处理静止trace

概述

在spring boot 2.2 中 默认状态为status 999

    private void addStatus(Map<String, Object> errorAttributes, RequestAttributes requestAttributes) {
        Integer status = (Integer)this.getAttribute(requestAttributes, "javax.servlet.error.status_code");
        if (status == null) {
            errorAttributes.put("status", 999);
            errorAttributes.put("error", "None");
        } else {
            errorAttributes.put("status", status);

            try {
                errorAttributes.put("error", HttpStatus.valueOf(status).getReasonPhrase());
            } catch (Exception var5) {
                errorAttributes.put("error", "Http Status " + status);
            }

        }
    }

如果我们自定义异常信息, 默认会打印一串trace信息,但是我们不需要

 

 

 解决办法:

 

 

@Component
public class AppErrorAttribute extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace); // 这里参数可以配置为false
        map.put("url","www.blogdgw.com");
        map.put("ext",webRequest.getAttribute("ext",0));
        // 禁止trace 覆盖 
        //map.put("trace","");
        return map;
    }
}

 

posted @ 2019-12-03 18:27  ---dgw博客  阅读(1478)  评论(0编辑  收藏  举报