ClassCastException异常? 400 报错
我在使用JedisCluster的实例,调用它的方法getClusterNodes()然后给一个JedisPool赋值的时候,提示我一个ClassCastException,具体的栈信息如下:
java.lang.ClassCastException: redis.clients.jedis.JedisPool cannot be cast to redis.clients.jedis.JedisPool at com.redis.monitor.redis.impl.RedisServiceImpl.ping(RedisServiceImpl.java:537) at com.redis.monitor.manager.impl.RedisManagerImpl.ping(RedisManagerImpl.java:190) at com.redis.monitor.web.interceptor.ServerInteceptor.preHandle(ServerInteceptor.java:93) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:891) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) at javax.servlet.http.HttpServlet.service(HttpServlet.java:693) at javax.servlet.http.HttpServlet.service(HttpServlet.java:806) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:533) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1351) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1322) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:473) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:514) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:920) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:403) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:184) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:856) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:114) at org.eclipse.jetty.server.Server.handle(Server.java:352) at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:596) at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1049) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:590) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:212) at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:426) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:510) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.access$000(SelectChannelEndPoint.java:34) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:450) at java.lang.Thread.run(Thread.java:722)代码如下:
public String ping(Redis redis) { Jedis jedis = null; JedisPool jp = null; boolean broken = false; try { Map<String, JedisPool> poolMap = redis.getJedisCluster().getClusterNodes(); for(JedisPool jp1:poolMap.values()){ jedis = jp1.getResource(); jedis.ping(); } } catch (JedisConnectionException jce) { broken = true; } catch (Exception e) { e.printStackTrace(); } return ""; }
在for循环赋值给JedisPool的时候会出错.
大家帮忙看看是怎么回事
我在上边对JedisPool进行了一下构造,失败了,也报了一个异常:
Causedby:java.lang.NoSuchMethodError:redis.clients.jedis.JedisPool:method<init>()Vnotfoundatcom.redis.monitor.redis.impl.RedisServiceImpl.ping(RedisServiceImpl.java:530)atcom.redis.monitor.manager.impl.RedisManagerImpl.ping(RedisManagerImpl.java:190)atcom.redis.monitor.web.interceptor.ServerInteceptor.preHandle(ServerInteceptor.java:93)atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:891)...31more我在上边对JedisPool进行了一下构造,失败了,也报了一个异常:
Causedby:java.lang.NoSuchMethodError:redis.clients.jedis.JedisPool:method<init>()Vnotfoundatcom.redis.monitor.redis.impl.RedisServiceImpl.ping(RedisServiceImpl.java:530)atcom.redis.monitor.manager.impl.RedisManagerImpl.ping(RedisManagerImpl.java:190)atcom.redis.monitor.web.interceptor.ServerInteceptor.preHandle(ServerInteceptor.java:93)atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:891)...31more谢谢哥,原来问题是这样的,我在项目里用到了ant和maven,ant打包的时候会把maven中的那些jar包都放到WEB-INF下的lib文件夹,然后我在eclipse测试测试的时候,虽然是用maven构建的项目,但项目依然会进入lib文件夹读取jar包.
而我确实升级了一下jedis的版本,从2.1.0->2.7.3,导致有两个jar包依赖.结果2.7.3的类构建的对象又被赋值给2.1.0版本的类声明的变量,结果出错了.
解决方案就是,我把lib下的包都删掉了.然后在ant打包的时候,加了删除的内容,保证lib包的内容始终跟开发项目里的内容同步.搞定.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。