Home > Mobile >  Cannot resolve method 'getStackTrace' in 'ExceptionUtils' error
Cannot resolve method 'getStackTrace' in 'ExceptionUtils' error

Time:07-16

I am trying to build a global exception class and followed this example.

@Slf4j(topic = "GLOBAL_EXCEPTION_HANDLER")
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    // code omitted

    private ResponseEntity<Object> buildErrorResponse(Exception exception,
                                                      String message,
                                                      HttpStatus httpStatus,
                                                      WebRequest request) {
        ErrorResponse errorResponse = new ErrorResponse(httpStatus.value(), message);
        if (printStackTrace && isTraceOn(request)) {
            errorResponse.setStackTrace(ExceptionUtils.getStackTrace(exception));//!
        }
        return ResponseEntity.status(httpStatus).body(errorResponse);
    }
}

However, the errorResponse.setStackTrace(ExceptionUtils.getStackTrace(exception)); line gives "Cannot resolve method 'getStackTrace' in 'ExceptionUtils'" error. I checked related maven references and there is nothing missed in my project. May it be related to the following library that is inferred when I mouse over ExceptionUtils ?

org.apache.tomcat.util
Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.64 (tomcat-embed-core-9.0.64.jar)

CodePudding user response:

Looks like you don't have apache commons in maven dependencies. Try to add

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.0</version>
</dependency>

CodePudding user response:

I solved the problem by using correct library: org.apache.commons.lang3.exception.

Formerly a wrong library was imported: org.apache.tomcat.util.ExceptionUtils

  • Related