Handling wrapped exceptions in spring mvc

I have spring mvc and jackson. When I get the wrong request, jackson mapping fails and an UnrecognizedPropertyException is thrown. I want to handle this exception using

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

however, spring wraps this exception in an HttpMessageConversionException, so the code above does not work. Is it possible in spring to handle Jackson-specific (or shared libraries) exceptions?

+6
source share
2 answers

, UnrecognizedPropertyException IOException. RequestResponseBodyMethodProcessor, @RequestBody ( , ) IOException ( ), HttpMessageNotReadableException. , HttpMessageConverter HttpMessageNotReadableException, read.

, ( , ).

, 4.3, Spring MVC ExceptionHandlerMethodResolver ( @ExceptionHandler) cause (. SPR-14291). , , HttpMessageNotReadableException,

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

. Spring MVC , HttpMessageNotReadableException, Throwable#getCause .


-4.3 HttpMessageNotReadableException, .

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

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

:

/**
 * Global exception handler for unhandled errors.
 * @author Varun Achar
 * @since 2.0
 * @version 1.0
 *
 */
public class Http500ExceptionResolver extends SimpleMappingExceptionResolver
{
    @Inject
    private ViewResolver resolver;

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
    {
        ModelAndView mv = new ModelAndView();
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        if(CommonUtil.isAjax(request))
        {
            MappingJackson2JsonView view = new MappingJackson2JsonView();
            view.setObjectMapper(JsonUtil.getObjectMapper());
            mv.addObject("responseMessage", "We had some problems while serving your request. We are looking into it");
            mv.addObject("responseCode", GenericResponse.ERROR.code());
            mv.addObject("success", false);
            mv.setView(view);
        }
        else
        {
            mv.setViewName(resolver.getView(ViewConstants.ERROR_PAGE));
        }
        return mv;
    }
}

:

   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
        <property name="order" value="0" />
    </bean>
    <bean id="securityExceptionResolver"
        class="com.trelta.commons.utils.security.SecurityExceptionResolver">
        <property name="order" value="1"></property>
        <property name="exceptionMappings">
            <map>
                <entry key="org.springframework.security.access.AccessDeniedException"
                    value="/common/accessDenied"></entry>
                <entry key="org.springframework.security.core.AuthenticationException"
                    value="/common/authenticationFailure"></entry>
            </map>
        </property>
    </bean>
    <bean id="http500ExceptionResolver"
            class="com.trelta.commons.utils.security.Http500ExceptionResolver">
            <property name="order" value="3" />
    </bean>

, Spring . , !

javadoc SimpleMappingExceptionResolver

0

Source: https://habr.com/ru/post/1695829/


All Articles