[Solved] Failed to invoke @ExceptionHandler method is reported after adding @ControllerAdvice

first of all. Using ControllerAdvice alone does not work properly. Need to cooperate with @EnableWebMvc.

@ControllerAdvice

@EnableWebMvc

pulbic class ExceptionControllerAdvice{

  @ExceptionHandler(NotFoundException.class)

  @ResponseBody

  public Map<String,Object> notFoundExc(NotFoundException exc,HttpServletRequest req){

  …….

  }

}

An error is reported during the startup of exception handling.

Error message:

Jul 04, 2016 11:00:11 AM org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver doResolveHandlerMethodException

Warning: Failed to invoke @ExceptionHandler method: public java.util.Map<java.lang.String, java.lang.Object> com.dooioo.modelException.ExceptionControllerAdvice.notFoundError(com.dooioo.modelException.NotFoundException,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134)

at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101)

at ……….. (omitted…)

The messageconverter processor has been configured in xx-servlet.xml.

 <bean id=“dyMessageConverter” class=“com.dooioo.web.converter.DyMappingJacksonHttpMessageConverter”/>

 <bean class=“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”>

        <!– Processing function for spring binding –>

        <property name=“webBindingInitializer”>

            <bean class=“com.dooioo.init.DefaultBindingInitializer”/>

        </property>

        <property name=“messageConverters”>

            <list>

                <bean class=“org.springframework.http.converter.ByteArrayHttpMessageConverter”/>

                <ref bean=“dyMessageConverter”/>

            </list>

        </property>

        <property name=“requireSession” value=“false”/>

        <property name=“order” value=“0”/>

    </bean>

Going through the information online to no avail.

Finally, it is found that there is a relationship with the order in the xml. <mvc:annotation-driven />.

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter  adapter must be declared before <mvc:annotation-driven />, otherwise the conversion of parameter types cannot be processed normally 

This problem is solved.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *