如何在 Spring Boot 中使用 ControllerAdvice 获得完整的堆栈跟踪?
How to get a full stacktrace with ControllerAdvice in Spring Boot?
我将 @ControllerAdvice 用于我的 REST 服务。但是我无法获得完整的堆栈跟踪并定义发生异常的位置。
我的控制器建议:
@ControllerAdvice
public class RestResponseEntityExceptionHandler
extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {Exception.class})
protected ResponseEntity<Object> handleException(
Exception ex, WebRequest request) {
String bodyOfResponse = "Internal error";
System.out.println(ex.toString());
return handleExceptionInternal(ex, bodyOfResponse,
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
}
}
如果发生异常,我只会收到:
WARN 72490 --- [nio-5054-exec-2]
.m.m.a.ExceptionHandlerExceptionResolver : Resolved
[java.lang.NullPointerException]
如何获得完整的堆栈跟踪信息?
您可以将 System.out.println(ex.toString());
替换为 ex.printStackTrace();
以了解异常发生的位置和原因。
如果你用的是日志,那就用log.info("{}", ex)
我将 @ControllerAdvice 用于我的 REST 服务。但是我无法获得完整的堆栈跟踪并定义发生异常的位置。
我的控制器建议:
@ControllerAdvice
public class RestResponseEntityExceptionHandler
extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {Exception.class})
protected ResponseEntity<Object> handleException(
Exception ex, WebRequest request) {
String bodyOfResponse = "Internal error";
System.out.println(ex.toString());
return handleExceptionInternal(ex, bodyOfResponse,
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
}
}
如果发生异常,我只会收到:
WARN 72490 --- [nio-5054-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.NullPointerException]
如何获得完整的堆栈跟踪信息?
您可以将 System.out.println(ex.toString());
替换为 ex.printStackTrace();
以了解异常发生的位置和原因。
如果你用的是日志,那就用log.info("{}", ex)