Как получить объект / параметры POST-запроса в методе @ExceptionHandler - PullRequest
0 голосов
/ 27 сентября 2019

У меня есть метод controllerAdvice / @ ExceptionHandler.Мне нужно получить доступ к параметрам запроса в моем методе ExceptionHandler.Я могу получить URI с помощью getRequestURI (), но не знаю, как получить параметры запроса POST. У меня есть некоторые параметры идентификатора, которые я должен отправить обратно с помощью ExceptionResponse.Как мне это сделать?

    @ExceptionHandler(HttpClientErrorException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public @ResponseBody MyResponse handleResourceNotFound(final HttpClientErrorException ex,
            final HttpServletRequest request) {
        String str = request.getRequestURI();
        System.out.println(str);
        MyResponse response = new MyResponse(ex.getResponseBodyAsString(), 0, "-1");
        return response;
    }

1 Ответ

0 голосов
/ 27 сентября 2019

Нет прямого подхода для этого.В моем случае мне понравилось следующее:

Контроллер

@PostMapping("/test/")
@ResponseBody
public boolean isExist(@RequestBody Person person, HttpServletRequest request) {
    request.setAttribute("requestBody", person);
    int i = 1/0;
    return person != null && person.isExist();
}

Обработчик исключений

@ExceptionHandler(HttpClientErrorException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody MyResponse handleResourceNotFound(final HttpClientErrorException ex,
        final HttpServletRequest request) {
    String str = request.getRequestURI();
    System.out.println(request.getAttribute("requestBody"));
    MyResponse response = new MyResponse(ex.getResponseBodyAsString(), 0, "-1");
    return response;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...