当前位置: 首页 > news >正文

Redis:ClassCastException【bug】

Redis:ClassCastException【bug】

  • 前言
  • 版权
  • Redis:ClassCastException【bug】
    • 错误产生
    • 相关资源
      • 控制器:UserController("/user")
      • 配置:RedisConfiguration
      • 实体类:User
      • 数据表:User
    • 解决
  • 最后

前言

2024-3-15 16:31:58

以下内容源自《【bug】》
仅供学习交流使用

版权

禁止其他平台发布时删除以下此话
本文首次发布于CSDN平台
作者是CSDN@日星月云
博客主页是https://blog.csdn.net/qq_51625007
禁止其他平台发布时删除以上此话

Redis:ClassCastException【bug】

错误产生

    @RequestMapping(path = "/status", method = RequestMethod.GET)@ResponseBodypublic ResponseModel getUser(String token) {User user = null;if (StringUtils.isNotEmpty(token)) {user = (User) redisTemplate.opsForValue().get(token);}return new ResponseModel(user);}

有时候会报错

java.lang.ClassCastException: com.jsss.entity.User cannot be cast to com.jsss.entity.User

在这里插入图片描述

如果重新启动项目
就没有了报错
前端刷新页面
传入token还能拿到user的json串

相关资源

控制器:UserController(“/user”)

    @RequestMapping(path = "/status", method = RequestMethod.GET)@ResponseBodypublic ResponseModel getUser(String token) {User user = null;if (StringUtils.isNotEmpty(token)) {user = (User) redisTemplate.opsForValue().get(token);}return new ResponseModel(user);}

配置:RedisConfiguration

package com.jsss.configuration;import com.jsss.common.FastJsonSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfiguration {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// key serializertemplate.setKeySerializer(RedisSerializer.string());template.setHashKeySerializer(RedisSerializer.string());// value serializerFastJsonSerializer fastJsonSerializer = new FastJsonSerializer();template.setValueSerializer(fastJsonSerializer);template.setHashValueSerializer(fastJsonSerializer);template.afterPropertiesSet();return template;}}

实体类:User

package com.jsss.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;import java.io.Serializable;
import java.sql.Timestamp;/*** 用户账户:用于保存用户登录信息(User)表实体类** @author jsss*@since 2024-2-29*/@Data
@Setter
@Getter
@Accessors(chain = true)
@TableName("user")
public class User implements Serializable {/*** 用户ID:[0,8388607]用户获取其他与用户相关的数据*/@TableId(value = "user_id", type= IdType.AUTO)private Integer userId;/*** 账户状态:[0,10](1可用|2异常|3已冻结|4已注销)*/@TableField("state")private Integer state;/*** 所在用户组:[0,32767]决定用户身份和权限*/@TableField("user_group")private String userGroup;/*** 上次登录时间:*/@TableField("login_time")private Timestamp loginTime;/*** 手机号码:[0,11]用户的手机号码,用于找回密码时或登录时*/@TableField("phone")private String phone;/*** 手机认证:[0,1](0未认证|1审核中|2已认证)*/@TableField("phone_state")private Integer phoneState;/*** 用户名:[0,16]用户登录时所用的账户名称*/@TableField("username")private String username;/*** 昵称:[0,16]*/@TableField("nickname")private String nickname;/*** 密码:[0,32]用户登录所需的密码,由6-16位数字或英文组成*/@TableField("password")private String password;/*** 邮箱:[0,64]用户的邮箱,用于找回密码时或登录时*/@TableField("email")private String email;/*** 邮箱认证:[0,1](0未认证|1审核中|2已认证)*/@TableField("email_state")private Integer emailState;/*** 头像地址:[0,255]*/@TableField("avatar")private String avatar;/*** 创建时间:*/@TableField("create_time")private Timestamp createTime;}

数据表:User

-- auto-generated definition
create table user
(user_id     mediumint unsigned auto_increment comment '用户ID:[0,8388607]用户获取其他与用户相关的数据'primary key,state       smallint unsigned default '1'               not null comment '账户状态:[0,10](1可用|2异常|3已冻结|4已注销)',user_group  varchar(32)                                 null comment '所在用户组:[0,32767]决定用户身份和权限',login_time  timestamp         default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '上次登录时间:',phone       varchar(11)                                 null comment '手机号码:[0,11]用户的手机号码,用于找回密码时或登录时',phone_state smallint unsigned default '0'               not null comment '手机认证:[0,1](0未认证|1审核中|2已认证)',username    varchar(16)       default ''                not null comment '用户名:[0,16]用户登录时所用的账户名称',nickname    varchar(16)       default ''                null comment '昵称:[0,16]',password    varchar(64)       default ''                not null comment '密码:[0,32]用户登录所需的密码,由6-16位数字或英文组成',email       varchar(64)       default ''                null comment '邮箱:[0,64]用户的邮箱,用于找回密码时或登录时',email_state smallint unsigned default '0'               not null comment '邮箱认证:[0,1](0未认证|1审核中|2已认证)',avatar      varchar(255)                                null comment '头像地址:[0,255]',create_time timestamp         default CURRENT_TIMESTAMP not null comment '创建时间:'
)comment '用户账户:用于保存用户登录信息';

解决

参考这个
https://learn.skyofit.com/archives/2334
好像是一些字段的类型不是Integer和String,就会有类型转换的问题。

还没有尝试解决

最后

我们都有光明的未来

祝大家考研上岸
祝大家工作顺利
祝大家得偿所愿
祝大家如愿以偿
点赞收藏关注哦

http://www.lryc.cn/news/322246.html

相关文章:

  • JSON 配置文件
  • 由浅到深认识Java语言(6):控制流程语句
  • lv17 安防监控项目实战 3
  • 文本处理基本方法
  • Java面试题(Spring篇)
  • 操作系统:malloc与堆区内存管理
  • javaSwing推箱子游戏
  • JAVA多线程之JMM
  • Windows10 专业版 系统激活
  • C#使用LINQ和EF Core
  • 数字人解决方案— SadTalker语音驱动图像生成视频原理与源码部署
  • HTML5语法总结
  • 在github下载的神经网络项目,如何运行?
  • spring boot学习第十四篇:使用AOP编程
  • 凯特信安云签解决方案
  • 【xr806开发板使用】连接wifi例程实现
  • 停车管理系统asp.net+sqlserver
  • 新增多项功能,龙讯旷腾开源机器学习力场PWMLFF 2024.3版本上线
  • Docker常用命令练习
  • Kafka(十)安全
  • 流畅的 Python 第二版(GPT 重译)(四)
  • windows docker
  • 中国1km分辨率逐月地表太阳辐射均值数据集(1960-2022)
  • Android中内存泄漏的检测,解决方案以及示例
  • Android静默安装一(Root版)
  • 【漏洞复现】2.Apache Log4j2远程代码执行漏洞(CVE-2021-44228)复现及分析
  • Simulink|局部遮荫下光伏组件多峰值PSO-MPPT控制
  • 今日早报 每日精选15条新闻简报 每天一分钟 知晓天下事 3月21日,星期四
  • 蓝桥杯Python B组练习——完美的代价
  • 分布式游戏服务器