对于用 @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);
}
}
抛出异常,如抛出新的InternalErrorException(“error”,e)
,永远不会在控制台中打印堆栈跟踪,除非我删除注释@ResponseStatus
如何在保留注释 @ResponseStatus
的同时打印它?
最佳答案
参见Annotation Type ResponseStatus API doc .
Warning: when using this annotation on an exception class, or when setting the reason attribute of this annotation, the HttpServletResponse.sendError method will be used.
With HttpServletResponse.sendError, the response is considered complete and should not be written to any further. Furthermore, the Servlet container will typically write an HTML error page therefore making the use of a reason unsuitable for REST APIs. For such cases it is preferable to use a ResponseEntity as a return type and avoid the use of @ResponseStatus altogether.
HttpServletResponse.sendError 不会抛出您的错误,我想因此永远不会记录它。
也许您想为该异常实现异常处理程序以将其记录下来。
关于java - Spring Boot 2.3 中不打印自定义异常的 Stacktrace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62468008/