How to access _path property within HttpMessageNotReadableException

Viewed 315

l would like to access the _path property within HttpMessageNotReadableException, but I do not know from which method or methods to do this. How can I access it?

My JDK version is JDK8 and My Spring Boot version is 2.1.4.

2 Answers

This is how I would do it:

e.getHttpInputMessage().getHeaders().getLocation().getPath()

I'll leave it to you to check for null where necessary.

You can access it via ((MismatchedInputException)exception.getCause()).getPath() where exception is of type HttpMessageNotReadableException

Of course this depends on the cause being of type MismatchedInputException, I am not 100% sure about all possible cause types, but you can essentially define individual logic for each possible type to extract _path whenever possible.

I was able to captured and investigate _path within the following exception handler method;

@ExceptionHandler(HttpMessageNotReadableException.class)
public void handleBadRequestException(HttpMessageNotReadableException exception) {
    ((MismatchedInputException)exception.getCause()).getPath()  // do whatever with the result
    ...
}

But better to be safe about the possibility of null, maybe cause can be null, so add a null check for that before getting _path

Related