Как напечатать имя метода контроллера в @ControllerAdvice?

Я прошел по ссылке: Как найти имя контроллера в @controllerAdvice или @RestControllerAdvice в Spring MVC? много раз, но я хочу получить имя метода Controller в классе @ControllerAdvice.

Вот мой класс CustomHandler.

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

person Pra_A    schedule 23.01.2020    source источник


Ответы (2)


HandlerMethod должен дать вам эту информацию. Цитирование документации:

Инкапсулирует информацию о методе обработчика, состоящем из метод и бин. Обеспечивает удобный доступ к параметрам метода, возвращаемому значению метода, аннотациям метода и т. д.

Итак, вы можете использовать:

Method method = handlerMethod.getMethod();
person cassiomolin    schedule 23.01.2020

вы можете узнать имя метода из RuntimeException, чтобы это было

 e.getStackTrace()[0].getMethodName();
person laserany    schedule 23.01.2020