![](/img/trans.png)
[英]spring mvc controller error java.lang.IllegalStateException: No suitable resolver for argument [0]
[英]Spring 4 Exception Handling : No suitable resolver for argument
問題陳述
從Spring 3
遷移到Spring 4
在異常處理流程中引發一些異常。 異常說org.springframework.web.method.support.InvocableHandlerMethod
類中No suitable resolver for argument
。
因此,無論何時發生異常, Spring
嘗試找到它所獲得的異常處理程序,但是當它試圖填充方法參數或異常處理程序時,它將引發以下異常
無法調用@ExceptionHandler方法:
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]
HandlerMethod詳細信息:
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)
它基本上來自@CRequestParam("p") String p
變量
碼
調節器
@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");
}
異常處理程序
@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;
}
注釋
@Target( { ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CRequestParam {
String value() default "";
}
參數解析器
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;
}
XML配置
<bean
class="com.mkyong.common.resolver.AnnotationMethodHandlerAdapterConfigurer"
init-method="init">
<property name="customArgumentResolvers">
<list>
<bean class="com.mkyong.common.resolver.CRequestparamResolver" />
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
<property name="customArgumentResolvers">
<list>
<bean class="com.mkyong.common.resolver.CRequestparamResolver" />
</list>
</property>
</bean>
源代碼
通過在請求本身中傳遞自定義參數來解決該問題。
代碼如下:
調節器
@RequestMapping(method = RequestMethod.GET, value = "/exception2")
public String getException1(ModelMap model, @CRequestParam("p") String p, HttpServletRequest request) {
System.out.println("Exception 2 " + p);
request.setAttribute("p", p);
throw new CustomGenericException("1", "2");
}
異常處理程序
@ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex, HttpServletRequest request) {
ModelAndView model2 = new ModelAndView("error/generic_error");
model2.addObject("exception", ex);
System.out.println(request.getAttribute("p"));
System.out.println("CustomGenericException ");
return model2;
}
完整的源代碼可以在git上找到
通過提供WebApplicationInitializer的實現解決了該問題
public class SpringDispatcherConfig implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext Contianer = new AnnotationConfigWebApplicationContext();
Contianer.register(SpringConfig.class);
Contianer.setServletContext(servletContext);
DispatcherServlet dispatcherServlet = new DispatcherServlet(Contianer);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
Dynamic servlet = servletContext.addServlet("spring",dispatcherServlet);
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.