简体   繁体   English

Spring Boot 2.3 中未打印自定义异常的堆栈跟踪

[英]Stacktrace of custom exception is not printed in Spring Boot 2.3

Error stacktrace is not printed in console for the custom exception that is annotated with @ResponseStatus对于使用@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);
  }
}

Throwing exception like throw new InternalErrorException("error", e) , never get the stacktrace printed in the console unlesss I remove the annotation @ResponseStatus抛出异常,如throw new InternalErrorException("error", e) ,除非我删除注释@ResponseStatus ,否则永远不会在控制台中打印堆栈跟踪

How could I get it printed while keeping the annotation @ResponseStatus ?在保留注释@ResponseStatus的同时如何打印它?

See Annotation Type ResponseStatus API doc .请参阅注释类型响应状态 API 文档

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.警告:在异常 class 上使用此注解时,或在设置此注解的原因属性时,将使用 HttpServletResponse.sendError 方法。

With HttpServletResponse.sendError, the response is considered complete and should not be written to any further.使用 HttpServletResponse.sendError,响应被认为是完整的,不应再写入任何内容。 Furthermore, the Servlet container will typically write an HTML error page therefore making the use of a reason unsuitable for REST APIs.此外,Servlet 容器通常会写入 HTML 错误页面,因此使用了不适合 REST API 的原因。 For such cases it is preferable to use a ResponseEntity as a return type and avoid the use of @ResponseStatus altogether.对于这种情况,最好使用 ResponseEntity 作为返回类型,并完全避免使用 @ResponseStatus。

HttpServletResponse.sendError does not throw your error and I guess it is never logged because of that. HttpServletResponse.sendError 不会抛出你的错误,我猜它永远不会因此而被记录。

Maybe you want to implement exception handler for that exception to get it logged.也许您想为该异常实现异常处理程序以记录它。

Related question相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM