0
点赞
收藏
分享

微信扫一扫

Redis:ClassCastException【bug】

祈澈菇凉 03-16 13:00 阅读 2
redisbug

Redis:ClassCastException【bug】

前言

2024-3-15 16:31:58

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

版权

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

Redis:ClassCastException【bug】

错误产生

    @RequestMapping(path = "/status", method = RequestMethod.GET)
    @ResponseBody
    public 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)
    @ResponseBody
    public 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 {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();

        template.setConnectionFactory(factory);

        // key serializer
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());

        // value serializer
        FastJsonSerializer 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,就会有类型转换的问题。

还没有尝试解决

最后

我们都有光明的未来

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

举报

相关推荐

0 条评论