redis通用配置類的使用詳解
redis通用配置類
作用 處理Springboot使用 RedisTemplate過程中的編碼問題
現(xiàn)象如下,看數(shù)據(jù)的時候不方便

所以添加一下的配置類之后,就可以了
package com.htb.beidawebspringboot10redis.config;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
/**
* @Description:Redis通用配置類
* @Author 16221
* @Date 2020/4/23
**/
@Configuration
public class RedisConfig {
@Bean
//不指定id的話bean 的id就是方法名
//返回結(jié)果就是spring中管理的bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//ObjectMapper 指定在轉(zhuǎn)成json的時候的一些轉(zhuǎn)換規(guī)則
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//把自定義objectMapper設(shè)置到j(luò)ackson2JsonRedisSerializer中(可以不設(shè)置,使用默認規(guī)則)
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//RedisTemplate默認的序列化方式使用的是JDK的序列化
//設(shè)置了key的序列化方式
template.setKeySerializer(new StringRedisSerializer());
//設(shè)置了value的序列化方式
template.setValueSerializer(jackson2JsonRedisSerializer);
return template;
}
}
原理
設(shè)置其他的序列化方式使用json形式
RedisTemplate,默認序列化的時候,用的RedisTemplate里面的一個RedisSerializer對象的string方法

看string()方法


轉(zhuǎn)成了byte[] bytes
就是說最終是轉(zhuǎn)成了字節(jié)流
所以并不是通過json串的方式,這樣出來的結(jié)果就不是json串
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Linux服務(wù)器使用Redis作為數(shù)據(jù)緩存并用log4j2進行日志記錄的過程分享
這篇文章主要介紹了Linux服務(wù)器使用Redis作為數(shù)據(jù)緩存并用log4j2日志記錄,關(guān)于SpringBoot項目配置Redis與log4j2是查詢官方文檔,本文中的Redis配置類、Redis工具類以及l(fā)og4j2.xml配置文件來自網(wǎng)絡(luò),查證源自何處比較麻煩,所以在此感謝所有人的分享2023-09-09
Redis?緩存淘汰策略和事務(wù)實現(xiàn)樂觀鎖詳情
這篇文章主要介紹了Redis緩存淘汰策略和事務(wù)實現(xiàn)樂觀鎖詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
Redis的復(fù)制延遲優(yōu)化的實現(xiàn)步驟
本文探討了Redis主從復(fù)制架構(gòu)中的復(fù)制延遲問題及其優(yōu)化策略,復(fù)制延遲主要由網(wǎng)絡(luò)延遲、節(jié)點性能不足和數(shù)據(jù)量過大等因素導(dǎo)致,下面就來介紹一下優(yōu)化方案,感興趣的可以了解一下2025-10-10
Redis使用LocalStorage的實現(xiàn)示例
本文介紹了如何在 NestJS 項目中參考 Redis 緩存接口封裝 LocalStorage,提供了 CacheUtil 類的詳細實現(xiàn)及使用示例,方便前端同學(xué)向全棧發(fā)展2026-04-04
Redis中Key過期時間的設(shè)置與應(yīng)用方式
文章主要介紹了Redis中設(shè)置和管理Key過期時間的多種命令,包括EXPIRE、PEXPIRE、EXPIREAT、PEXPIREAT、SET命令結(jié)合EX或PX參數(shù)、SETEX命令,以及獲取剩余過期時間的TTL和PTTL命令,最后通過緩存、會話管理和分布式鎖等應(yīng)用場景展示了這些命令的實際應(yīng)用2025-11-11
Redisson實現(xiàn)Redis分布式鎖的幾種方式
本文在講解如何使用Redisson實現(xiàn)Redis普通分布式鎖,以及Redlock算法分布式鎖的幾種方式的同時,也附帶解答這些同學(xué)的一些疑問,感興趣的可以了解一下2021-08-08

