SpringBoot错误返回数据不包含exception类名

解决办法点这里

又是一个学SpringBoot的时候遇到的因为1和2版本差异造成的坑2333,看源码多多益善嘛

定制含有json格式内容的错误页面的时候,无法获取到异常类:
在这里插入图片描述
去查了下SpringBoot是如何默认配置错误处理的,首先还是ctrl+N跳到自动配置类:
在这里插入图片描述
在这里插入图片描述
应该是你没错了,点进去看看这货的源码,里面包含两个请求的mapping方法(一个处理浏览器请求返回html错误页面,一个处理客户端请求返回其他类型的错误信息,例如json):

@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {

查看任一方法体,不难注意到会调用getErrorAttributes方法将错误信息存到Map类型的model中:

Map<String, Object> model = Collections
				.unmodifiableMap(getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));

于是再点进getErrorAttributes方法查看其源码:

protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
		WebRequest webRequest = new ServletWebRequest(request);
		return this.errorAttributes.getErrorAttributes(webRequest, includeStackTrace);
	}

被覆写了,再点返回的同名方法,跳到这个接口:

public interface ErrorAttributes {

然后ctrl+alt+B直接跳至实现类DefaultErrorAttributes,找到getErrorAttributes方法的实现:

public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> errorAttributes = new LinkedHashMap();
        errorAttributes.put("timestamp", new Date());
        this.addStatus(errorAttributes, webRequest);
        this.addErrorDetails(errorAttributes, webRequest, includeStackTrace);
        this.addPath(errorAttributes, webRequest);
        return errorAttributes;
    }

可以看到返回的数据包括时间戳、状态码、请求路径以及其它详细信息,所以接下来查看addErrorDetails方法的实现:

private void addErrorDetails(Map<String, Object> errorAttributes, WebRequest webRequest, boolean includeStackTrace) {
        Throwable error = this.getError(webRequest);
        if (error != null) {
            while(true) {
                if (!(error instanceof ServletException) || error.getCause() == null) {
                    if (this.includeException) {
                        errorAttributes.put("exception", error.getClass().getName());
                    }

                    this.addErrorMessage(errorAttributes, error);
                    if (includeStackTrace) {
                        this.addStackTrace(errorAttributes, error);
                    }
                    break;
                }

                error = error.getCause();
            }
        }

        Object message = this.getAttribute(webRequest, "javax.servlet.error.message");
        if ((!StringUtils.isEmpty(message) || errorAttributes.get("message") == null) && !(error instanceof BindingResult)) {
            errorAttributes.put("message", StringUtils.isEmpty(message) ? "No message available" : message);
        }

    }

注意其中这个if:

if (this.includeException) {
	errorAttributes.put("exception", error.getClass().getName());
}

拨云见日,答案很明显了,只有在includeException的值为true时,返回数据中才会加入异常类的类名。在实现类的开头也可以看到这个属性的相关配置,很容易得知其默认为false,需要将其置为true:

private final boolean includeException;

public DefaultErrorAttributes() { this(false); }


所以我们只需在配置文件里加上这一句:
server.error.include-exception=true

再看下错误页面:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值