java - Spring Boot 2.3 中不打印自定义异常的 Stacktrace

标签 java spring spring-boot logging

对于用 @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 不会抛出您的错误,我想因此永远不会记录它。

也许您想为该异常实现异常处理程序以将其记录下来。

Related question

关于java - Spring Boot 2.3 中不打印自定义异常的 Stacktrace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62468008/

相关文章:

spring - 错误 : getWriter() has already been called for this response in springmvc

java - Jackson xml 和 json 根元素

java - 调用 getter 方法时出现 NullPointerException

java - JX-Browserview重叠系统现有内容

java - 什么是 Response.ListResponse?

java - JMS持久异步事件通知

java - 为什么在 junit5 测试中 Autowiring 的 Controller 总是为空?

java - 了解 spring-security-oauth2 @EnableAuthorizationServer

java - 防止在 JSP 页面加载时执行 SQL 语句

java - Azure IoT 中心设备消息、消息正文上的路由筛选器不起作用