NutzCN Logo
问答 json转换的问题报java.lang.ClassCastException异常
发布于 13天前 作者 qq_93105859 232 次浏览 复制 上一个帖子 下一个帖子
标签:

转换的对象如下:


public class Session implements Serializable{ /** * */ private static final long serialVersionUID = -3623518883696273400L; /** * 在会话对象中保存的内容 */ private Map<Object, Object> sessionContent; public void setAttribute(Object k, Object v) { if (k != null && v != null) { this.sessionContent.put(k, v); } } }

我先将这个对象保存在另一台服务器的内存中,后面在需要用到这个对象的时候,通过http取回来,代码如下:

/**
	 *
	 * Description : 通过sessionId获得session对象
	 * 
	 * @author : ChenBin
	 * @date : 2019年9月7日 下午2:56:48
	 */
	public static Session get(String sessionId) {
		Map<String, Object> params = new HashMap<>();
		params.put("sessionId", sessionId);
		String json = Http.post(GET, params, Config.CACHE_TIMEOUT);
		return Json.fromJson(Session.class, json);
	}

在另外一个地方调用了这个 get(String sessionId) 方法,代码如下:

...
                Session session = SC.get(sessionId);
		if (session == null)
			return false;
		BaseUser user = (BaseUser) session.getAttribute(AK.S_USER);// 此处报错
		if (user == null)
			return false;
		return true;
...

报错内容如下:

java.lang.ClassCastException: org.nutz.lang.util.NutMap cannot be cast to com.zq.ums.common.entity.dao.tables.base.BaseUser
	at com.zq.ums.biz.mvc.CheckSessionProcessor.isSessionValid(CheckSessionProcessor.java:146)
	at com.zq.ums.biz.mvc.CheckSessionProcessor.checkSession(CheckSessionProcessor.java:64)

	at com.zq.ums.biz.mvc.CheckSessionProcessor.process(CheckSessionProcessor.java:49)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at com.zq.ums.biz.web.filter.BizFilter.doFilter(BizFilter.java:49)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.zq.ums.biz.web.filter.CrossFilter.doFilter(CrossFilter.java:65)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

找了很久没找到解决方案,麻烦帮我看一下,谢谢啦

1 回复

sessionContent的类型信息是Object,没有具体的类型信息,Json.fromJson的时候就无法直接转换成BaseUser对象,然后user对象在json里面是表达成map的,所以会默认转成NutMap对象

通过map2obj转换一下就好了

BaseUser user = Lang.map2obj((NutMap)session.getAttr(xxxx),BaseUser.class);

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