为什么在Spring中扩展ResponseEntityExceptionHandler时堆栈跟踪没有被打印出来?

huangapple go评论77阅读模式
英文:

Why is the stackTrace not printed when extending the ResponseEntityExceptionHandler in Spring?

问题

我正在构建一个Spring Boot应用程序。我试图使用一种合理的方式来处理异常引发时的REST响应。所以我扩展了ResponseEntityExceptionHandler为一个名为RestResponseEntityExceptionHandler的类。
问题是,当抛出异常时,堆栈跟踪未在控制台中打印。当我删除RestResponseEntityExceptionHandler类时,堆栈跟踪再次打印在控制台上!

以下是RestResponseEntityExceptionHandler类的样子:

@RestControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(value = { IllegalArgumentException.class, TechnicalException.class})
    protected void handleBadRequest(RuntimeException e, HttpServletResponse response) throws IOException 
    {
        response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }
}

我正在使用logback进行日志记录。

我找到了一些处理方法,比如添加logger.error("Details : ", exception);,它可以正常工作并打印堆栈跟踪,但我更喜欢不使用这样的解决方案,因为它仅适用于该类处理的异常...其他异常不会打印堆栈跟踪。

为什么堆栈跟踪没有打印的任何解释?先感谢您。

英文:

I'm building a spring boot application. I'm trying to use a decent way of handling rest responses when an exceptions is raised. So I extended the ResponseEntityExceptionHandler into a class named RestResponseEntityExceptionHandler.
The problem is that when an exception is thrown, the stackTrace is not printed in the console. when I delete this RestResponseEntityExceptionHandler class, the stacktrace is printed again in the console !

Here is what the RestResponseEntityExceptionHandler class looks like :

@RestControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

	@ExceptionHandler(value = { IllegalArgumentException.class, TechnicalException.class})
	protected void handleBadRequest(RuntimeException e, HttpServletResponse response) throws IOException 
    {
		response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
	}
}

I am using logback for logging.

I found some tricks to deal with that, like adding a logger.error("Details : ", exception); which works fine and prints the stackTrace but I prefer not to use a solution like that since it works only for the exceptions handeled by that class... the other exceptions wont print the stackTrace.

Any explanations why the stackTrace is not printed ?
Thanx in advance.

答案1

得分: 1

因为您正在处理异常。如果您想要在处理过程中打印信息,请将logger放在ExceptionHandler方法内部。

英文:

Because You are handling Exception. If you want to print along with the handling, put logger inside ExceptionHandler methods.

答案2

得分: 0

因为您已经在RestResponseEntityExceptionHandler中处理了异常,所以不会“打印”该异常。

英文:

It is not "printed" because you are already handling the exception in RestResponseEntityExceptionHandler.

答案3

得分: 0

由于您在RestResponseEntityExceptionHandler中通过RestControllerAdvice处理ResponseEntityExceptionHandler,并且仅在响应中传递错误消息,因此堆栈跟踪未被打印出来。如果您想要特别打印它,那么请在handleBadRequest方法中为错误(即e)添加一个记录器。

英文:

Staketrace is not getting printed because you are handling the ResponseEntityExceptionHandler in the RestResponseEntityExceptionHandler by RestControllerAdvice and passing only the error message in response. It you want to print it specially then add a logger in your handleBadRequest method for the error i.e. e in your case.

huangapple
  • 本文由 发表于 2020年8月26日 02:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63585443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定