Skip to content
Advertisement

Handle an exception in another method without losing the request objects from original one

There is an Exception that is thrown when an user is not found in database and i’d like to handle that particular exception from the controller perspective layer in a separated method by @ExceptionHandler annotation without losing the original data sent by the user. Well, so, i’m using Sessions and my first attempt was trying to get the object back from it by HttpServletRequest but i got:

JavaScript

The code:

JavaScript

How would it be if i use a try-catch block

JavaScript

Here the simplified code:

JavaScript
  • Controller with POST.

The exception happens in the POST at line with: Ponto pontoConvertido = pontoConverter.convert(ponto);

JavaScript

Advertisement

Answer

You can add WebRequest (or HttpSession, etc…) as a parameter in your exception handler, it will be injected by Spring.

You can have a look at the documentation here to see what parameter can be injected by Spring when the handler is called.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement