【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;
    }
}
'setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)'的使用已经被弃用,并标记为即将移除。 目前最新版本的Jackson库是2.12.5,它不再建议使用该方法来设置ObjectMapper。相反,建议使用其他更现代化的方法来自定义ObjectMapper配置。 为了设置ObjectMapper,你可以使用以下方法之一: 1. 使用构造函数创建ObjectMapper对象,并在创建时传入适当的配置参数。 2. 使用ObjectMapper提供的setter方法对其属性进行设置。 3. 创建一个定制的ObjectMapper扩展类,并覆盖其中的方法以实现自定义配置。 具体的代码示例如下: 1)使用构造函数创建ObjectMapper对象: ``` ObjectMapper mapper = new ObjectMapper(); // 在构造函数中设置需要的配置参数 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ``` 2)使用setter方法设置ObjectMapper的属性: ``` ObjectMapper mapper = new ObjectMapper(); // 使用setter方法设置需要的配置参数 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); ``` 3)创建一个定制的ObjectMapper扩展类: ``` public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper() { // 在构造函数中进行自定义配置 configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } } CustomObjectMapper mapper = new CustomObjectMapper(); ``` 注意,在使用任何方法进行配置时,请根据你的具体需求选择适当的配置选项,并确保使用的Jackson库版本与你引用的Maven坐标一致。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [jackson-databind-2.12.5-API文档-中文版.zip](https://download.csdn.net/download/qq_36462452/85547895)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ObjectMapper使用详细介绍](https://blog.csdn.net/qq_45228323/article/details/126445836)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值