title: Spring 4 Exception Handling : No suitable resolver for argument
body: <p><strong>Problem Statement</strong></p>
  
  <p>Migration to <code>Spring 4</code> from <code>Spring 3</code> induces some exceptions in exception handling flow.
    The Exception says <code>No suitable resolver for argument</code> in the <code>org.springframework.web.method.support.InvocableHandlerMethod</code> class.</p>
  
  <p>So whenever and exception occurs <code>Spring</code> tries to find the Exception Handler which it gets
   but when it tries to populate the method arguments or exception Handler it throws the below exception</p>
  
  <p>Failed to invoke @ExceptionHandler method:</p>
  
  <pre><code>  public org.springframework.web.servlet.ModelAndView  
         HelloController.handleCustomException(CustomGenericException, javax.servlet.http.HttpServletRequest, org.springframework.web.servlet.ModelAndView)
  
    java.lang.IllegalStateException: 
       No suitable resolver for argument [2] 
               [type=org.springframework.web.servlet.ModelAndView]
  </code></pre>
  
  <p>HandlerMethod details:</p>
  
  <pre><code>Controller [HelloController]
  Method [public org.springframework.web.servlet.ModelAndView  
          HelloController.handleCustomException(CustomGenericException,
              javax.servlet.http.HttpServletRequest,org.springframework.web.servlet.ModelAndView)]
          at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(
                       InvocableHandlerMethod.java:169)
  </code></pre>
  
  <p>It basically comes for <code>@CRequestParam("p") String p</code>  variable</p>
  
  <p><strong>Code</strong></p>
  
  <p>Controller</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET, value="/exception2")
      public String getException1(ModelMap model, @CRequestParam("p") String p) {
  
          System.out.println("Exception 2 "+ p);
          throw new CustomGenericException("1","2");
      }
  </code></pre>
  
  <p>Exception Handler</p>
  
  <pre><code>@ExceptionHandler(CustomGenericException.class)
      public ModelAndView handleCustomException(CustomGenericException ex, 
              HttpServletRequest request, @CRequestParam("p") String p) {
  
              ModelAndView model = new ModelAndView("error/generic_error");
              model.addObject("exception", ex);
              System.out.println("CustomGenericException  ");
              return model;
      }
  </code></pre>
  
  <p>Annotations</p>
  
  <pre><code>@Target( { ElementType.PARAMETER })
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  public @interface CRequestParam {
      String value() default "";
  }
  </code></pre>
  
  <p>Param Resolver</p>
  
  <pre><code>public class CRequestparamResolver implements HandlerMethodArgumentResolver {
  
  
      @Override
          public boolean supportsParameter(MethodParameter methodParameter) {
            CRequestParam requestParamAnnotation = 
            methodParameter.getParameterAnnotation(CRequestParam.class);
          if(requestParamAnnotation==null){
          return false;
          }
          return true;
          }
  
      @Override
      public Object resolveArgument(MethodParameter methodParameter,
          ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
          WebDataBinderFactory binderFactory) throws Exception {
  
      CRequestParam requestParamAnnotation = methodParameter .getParameterAnnotation(CRequestParam.class);
  
      if (requestParamAnnotation != null) {
          String requestParamName = requestParamAnnotation.value();
          if (StringUtils.hasText(requestParamName)) {
          return webRequest.getParameter(requestParamName);
          }
      }
      return null;
    }
  </code></pre>
  
  <p>XML Configuration</p>
  
  <pre><code>&lt;bean
          class="com.mkyong.common.resolver.AnnotationMethodHandlerAdapterConfigurer"
          init-method="init"&gt;
          &lt;property name="customArgumentResolvers"&gt;
              &lt;list&gt;
                  &lt;bean class="com.mkyong.common.resolver.CRequestparamResolver" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean 
   class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"&gt;
          &lt;property name="customArgumentResolvers"&gt;
              &lt;list&gt;
                  &lt;bean class="com.mkyong.common.resolver.CRequestparamResolver" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p><strong>Source Code</strong> </p>
  
  <p><a href="https://github.com/santoshjoshi/SpringMVC4" rel="noreferrer">https://github.com/santoshjoshi/SpringMVC4</a></p>