0

I went through link: How to find controller name in @controllerAdvice or @RestControllerAdvice in Spring MVC? many times, but I am looking to get the Controller methodname in @ControllerAdvice class.

Here is my CustomHandler Class.

@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
    ........
    ..........
    ..........

    @ExceptionHandler({DataExistsException.class})
    public ResponseEntity<Object> handleDataExistsException(RuntimeException e, WebRequest request, HttpServletRequest httpRequest, HttpServletResponse response, HandlerMethod handlerMethod) {
        // Here I am looking to print Controller endpoint method name

        LoggingUtil.printJsonReq(httpRequest, HttpStatus.valueOf("BAD_REQUEST").value(), LoggingUtil.getTime(httpRequest), null, response);
        return handleExceptionInternal(e, getErrors(error), getHeaders(), HttpStatus.BAD_REQUEST, request);
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
PAA
  • 1
  • 46
  • 174
  • 282

2 Answers2

0

HandlerMethod should give you this information. Quoting the documentation:

Encapsulates information about a handler method consisting of a method and a bean. Provides convenient access to method parameters, the method return value, method annotations, etc.

So you can use:

Method method = handlerMethod.getMethod();
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

you can find out the method name from the RuntimeException so that would be

 e.getStackTrace()[0].getMethodName();
laserany
  • 1,257
  • 1
  • 8
  • 17