【SpringBoot+redis的序列化配置之Jackson2JsonRedisSerializer .setObjectMapperis报错】

报错信息

redis 设置序列化时报错 setObjectMapperis deprecated and marked for removal

具体错误信息截图:

'setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)' is deprecated and marked for removal

翻译过来的意思就是函数setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)已弃用并标记为要删除。

错误代码模块截图:
在这里插入图片描述

解决方法

参考Jackson2JsonRedisSerializer的官方文档https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/serializer/Jackson2JsonRedisSerializer.html
分析得到由于版本的升级此接口被启用,需要修改成最新版本支持的实现方式。
下面列出分析过程:

找到setObjectMapper对应位置

在这里插入图片描述
官方给出的解释为该接口已弃用,如果要使用原接口功能对Jackson2JsonRedisSerializer配置对象映射器,则使用对应的构造函数。

查看构造函数

在这里插入图片描述
可以看到直接使用箭头所指的构造函数即可实现配置对象映射器的功能。

修改方法

在这里插入图片描述

完整代码

此处贴上版本为3+时,redis设置序列化的完整代码:

package com.example.redistest.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        // 老版本,已被弃用,不使用此方法
        // Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        // jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class);

        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
首先,你需要在 Spring Boot 项目中添加 Redis 的依赖,可以使用以下 Maven 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接着,在 application.properties 或 application.yml 文件中添加 Redis配置: ```yaml # Redis 主节点配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 # Redis 从节点配置 spring.redis.cluster.nodes=192.168.1.1:6379,192.168.1.2:6379 spring.redis.cluster.master=master ``` 以上配置是一个 Redis 主节点和两个从节点的示例,其中: - `spring.redis.host` 和 `spring.redis.port` 是主节点的地址和端口。 - `spring.redis.cluster.nodes` 是从节点的地址和端口列表,多个节点使用英文逗号分隔。 - `spring.redis.cluster.master` 是主节点的名称,可以在 Redis 配置文件中设置。 最后,你需要使用 RedisTemplate 或者 StringRedisTemplate 类来操作 Redis。在 Spring Boot 中,你可以通过注入 RedisTemplate 或者 StringRedisTemplate 类来使用 Redis。例如: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } ``` 以上代码中,我们注入了 RedisTemplate 类,并使用其 `opsForValue` 方法来操作 Redis 中的数据。 注意,如果你使用的是 Redis 主从复制模式,你在写入数据时应该使用主节点,而在读取数据时可以使用主节点或从节点。如果你使用的是 Redis 集群模式,Spring Boot 会自动将读取操作负载均衡到不同的节点上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值