在spring mvc中,定義如下RequestMapping方法
@RequestMapping(value="queryList",method = RequestMethod.POST) @ResponseBody public String queryList(HttpSession httpSession,@RequestBody(required = false) List<String> taskIds){ String rtn = ""; … return rtn; }
以上方法在本地環境(window)和測試環境 (redhat)是可以正常被請求,但是發布到生產環境(debian8.8)被請求時,卻拋出以下異常:
2018-08-14 16:40:33.157 [https-jsse-nio-8443-exec-10] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/icc-interface] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Cannot generate variable name for non-typed Collection parameter type] with root cause java.lang.IllegalArgumentException: Cannot generate variable name for non-typed Collection parameter type at org.springframework.core.Conventions.getVariableNameForParameter(Conventions.java:119) at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:129) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
定位到源碼:拋出異常的原因是程序無法解析出集合中實際的類型,不知道什么原因集合中的真正類型丟失了,程序無法根據集合真正元素的類型生成對象實例
public static String getVariableNameForParameter(MethodParameter parameter) { Assert.notNull(parameter, "MethodParameter must not be null"); Class<?> valueClass; boolean pluralize = false; if (parameter.getParameterType().isArray()) { valueClass = parameter.getParameterType().getComponentType(); pluralize = true; } else if (Collection.class.isAssignableFrom(parameter.getParameterType())) { // 看源碼是程序無法解析出集合中實際的類型 valueClass = ResolvableType.forMethodParameter(parameter).asCollection().resolveGeneric(); if (valueClass == null) { throw new IllegalArgumentException( "Cannot generate variable name for non-typed Collection parameter type"); } pluralize = true; } else { … } … }
嘗試將JDK8升級到最新版本,但是問題還是存在,最后在網上找到如下的解決方法:
重新定義StringList 類,直接帶上集合的類型
public class StringList extends ArrayList<String> { }
修改方法:使用StringList替換List
@RequestMapping(value="queryList",method = RequestMethod.POST) @ResponseBody public String queryList(HttpSession httpSession,@RequestBody(required = false)StringList taskIds){ String rtn = ""; … return rtn; }
修正問題成功
---------------------
作者:hry2015
來源:CSDN
原文:https://blog.csdn.net/hry2015/article/details/81913638
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!