gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:13:32 30 4
gpt4 key购买 nike

对于用 @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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com