Kirill
Kirill

Reputation: 6036

How to get a full stacktrace with ControllerAdvice in Spring Boot?

I use a @ControllerAdvice for my REST service. However I can not get a full stacktrace and define where an exception occured.

My ControllerAdvice:

@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);

    }
}

If an Exception occurs I receive only:

WARN 72490 --- [nio-5054-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.NullPointerException]

How I can get a full stacktrace?

Upvotes: 1

Views: 1798

Answers (1)

Suraj
Suraj

Reputation: 767

You can replace System.out.println(ex.toString()); with ex.printStackTrace(); to get where the exception occured and cause of it.

If you are using log, then use log.info("{}", ex)

Upvotes: 3

Related Questions