【发布时间】:2020-06-19 10:12:19
【问题描述】:
对于使用@ResponseStatus 注释的自定义异常,不会在控制台中打印错误堆栈跟踪
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class InternalErrorException extends RuntimeException {
public InternalErrorException(String message) {
super(message);
}
public InternalErrorException(String message, Throwable throwable) {
super(message, throwable);
}
}
抛出像throw new InternalErrorException("error", e)这样的异常,除非我删除注释@ResponseStatus,否则永远不会在控制台中打印堆栈跟踪
如何在保留注释@ResponseStatus 的同时打印它?
【问题讨论】:
-
你有什么问题?
-
如何打印?
-
您在打印堆栈跟踪吗?你能分享你是你试图打印它的代码吗
-
当代码抛出未处理的异常时,异常会一直渗透到 Tomcat,然后将其记录下来。当异常用
@ResponseStatus注释时,Spring 拦截异常并将其转换为response.sendError(...)调用以生成具有给定状态代码的标准HTTP 响应,因此没有异常渗透到Tomcat,因此没有日志记录完毕。 Spring 代码可以记录异常,如果您设置warnLogCategory属性如前面的链接所示,但不记录堆栈跟踪。
标签: java spring spring-boot logging