Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have Spring MVC and jackson. When I start an incorrect request, Jackson mapping fails and UnrecognizedPropertyException is thrown. I want to handle this exception using

@ExceptionHandler
public String handle(UnrecognizedPropertyException e) {
  ...
}

However Spring wraps this exception in HttpMessageConversionException so the code above doesn't work. is it possible in Spring to handle Jackson specific (or in general library specific) exceptions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
109 views
Welcome To Ask or Share your Answers For Others

1 Answer

Unfortunately, UnrecognizedPropertyException is a subtype of IOException. The RequestResponseBodyMethodProcessor that handles the @RequestBody (I assume that's where the exception occurs) has special handling for IOException (interpreting as a failure of the request input stream), wrapping it in a HttpMessageNotReadableException. Additionally, the HttpMessageConverter interface is specified to throw HttpMessageNotReadableException if there is a conversion error during read.

You're going to have to deal with that no matter what (if Jackson threw unchecked exceptions instead, things might have been different).

Fortunately, since 4.3, Spring MVC's ExceptionHandlerMethodResolver (which processes @ExceptionHandler) can unwrap the cause of exceptions (see SPR-14291). As such, assuming you do not have a handler for any exceptions in the inheritance hierarchy of HttpMessageNotReadableException, your handler method

@ExceptionHandler
public String handle(UnrecognizedPropertyException e) {
    ...
}

will be used to handle the exception. This happens after Spring MVC looks for a handler method that could handle a HttpMessageNotReadableException, then unwraps the nested exception with Throwable#getCause and tries the lookup again.


In pre-4.3, or if you do have a handler for an exception type in HttpMessageNotReadableException's inheritance hierarchy, you can always delegate after extracting the cause yourself.

@ExceptionHandler
public String handle(HttpMessageConversionException e) throws Throwable {
    Throwable cause = e.getCause();
    if (cause instanceof UnrecognizedPropertyException) {
        handle((UnrecognizedPropertyException) cause);
    }
    ...
}

public String handle(UnrecognizedPropertyException e) {
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...