NutzCN Logo
问答 从NutMap中取String[]时,报错无法把对象转换成String[]
发布于 2284天前 作者 qq_7fafbecc 1617 次浏览 复制 上一个帖子 下一个帖子
标签:

问题描述:
从NutMap中接收参数,跟断点可以看到是String[]类型的参数typeCodes[]。
当typeCodes有2个以上的值的时候(例如:[1,2]),代码不报错;
当typeCodes只有1个值的时候(例如[1]),就报转换错误.
应该怎么解决这个问题?

代码:

if(paramMap.get("typeCodes[]")!=null ){
			String[] typeCodes = (String[])paramMap.get("typeCodes[]");
			String inStr = CommonUtil.getInStr(typeCodes);
			if(!"".equals(inStr)){
				...
			}
		}

报错信息

For the reason: java.lang.String cannot be cast to [Ljava.lang.String;
org.nutz.lang.InvokingException: Fail to invoke [com.shlx.blood.service.business.BagService$$NUTZAOP].queryBagInfo() by args:
 {bagCode=, bdCode=, typeCodes[]=1, page=1, rows=15}
For the reason: java.lang.String cannot be cast to [Ljava.lang.String;
  at org.nutz.lang.Invoking.invoke(Invoking.java:195)
  at org.nutz.lang.Mirror.invoke(Mirror.java:1100)
  at com.shlx.blood.service.ComServices.ExcuteServices(ComServices.java:137)
  at com.shlx.blood.action.business.BagAction.queryBagInfo(BagAction.java:51)
5 回复

getAsList或者getArray

不行啊,多个值OK,单个值的时候,还是报错

2018-01-17 15:47:37,731 com.shlx.blood.service.ComServices.ExcuteServices(ComServices.java:142) DEBUG - Fail to invoke [com.shlx.blood.service.business.BagService$$NUTZAOP].queryBagInfo() by args:
 {bagCode=, bdCode=, typeCodes[]=5, page=1, rows=15}
For the reason: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
org.nutz.lang.InvokingException: Fail to invoke [com.shlx.blood.service.business.BagService$$NUTZAOP].queryBagInfo() by args:
 {bagCode=, bdCode=, typeCodes[]=5, page=1, rows=15}
For the reason: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
	at org.nutz.lang.Invoking.invoke(Invoking.java:195)
	at org.nutz.lang.Mirror.invoke(Mirror.java:1100)
	at com.shlx.blood.service.ComServices.ExcuteServices(ComServices.java:137)
if(paramMap.get("typeCodes[]")!=null ){
			String[] typeCodes = paramMap.getArray("typeCodes[]", String.class);
			//String[] typeCodes = (String[])paramMap.get("typeCodes[]");
			String inStr = CommonUtil.getInStr(typeCodes);
			if(!"".equals(inStr)){
				......
			}
		}

用getAsList试试吧, 还不行就只能判断一下了

怎么判断呢?

params.get("typeCodes[]") 然后判断类型

添加回复
请先登陆
回到顶部