第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為 spring-boot redis 緩存配置配置一個(gè)新的序列化程序

為 spring-boot redis 緩存配置配置一個(gè)新的序列化程序

溫溫醬 2022-07-27 20:59:32
我一直在嘗試更改 spring-boot redis 緩存的默認(rèn)序列化程序,因?yàn)槲蚁霃?Default 更改為 Jackson2Json 實(shí)現(xiàn)之一。Jackson2Json 庫(kù)中有兩個(gè)實(shí)現(xiàn),其中之一是:GenericJackson2JsonRedisSerializer,我可以在以下 bean 實(shí)例化中使用它:@Bean@Primarypublic RedisCacheConfiguration defaultCacheConfig(ObjectMapper objectMapper) {    return RedisCacheConfiguration.defaultCacheConfig()        .serializeKeysWith(            SerializationPair.fromSerializer(                new StringRedisSerializer()            )        )        .serializeValuesWith(            SerializationPair.fromSerializer(                new GenericJackson2JsonRedisSerializer(objectMapper)            )        )        .prefixKeysWith("");}當(dāng)我使用此序列化程序時(shí),序列化工作正常,所有內(nèi)容都存儲(chǔ)在 redis 服務(wù)器上,但是當(dāng)我嘗試反序列化存儲(chǔ)在 redis 服務(wù)器上的 JSON 時(shí),我收到以下異常:java.util.LinkedHashMap cannot be cast to tutorial.Person with root causejava.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to tutorial.Person緩存的使用方式如下:@Cacheable(cacheNames = "person", key = "'person:'.concat(#post.id)")public Person findPostAuthor(Post post){}序列化程序不知道如何從 LinkedHashMap 轉(zhuǎn)換為 Person,我怎么能告訴他怎么做呢?我嘗試使用的另一個(gè)序列化器是 Jackson2JsonRedisSerializer:@Bean@Primarypublic RedisCacheConfiguration defaultCacheConfig(ObjectMapper objectMapper) {    Jackson2JsonRedisSerializer<Person> serializer = new Jackson2JsonRedisSerializer<>(Person.class);    serializer.setObjectMapper(objectMapper);    return RedisCacheConfiguration.defaultCacheConfig()        .serializeKeysWith(            SerializationPair.fromSerializer(                new StringRedisSerializer()            )        )        .serializeValuesWith(            SerializationPair.fromSerializer(                serializer            )        )        .prefixKeysWith("");}這樣,我必須為保存在 redis 緩存中的每個(gè)對(duì)象聲明一個(gè) bean,但我可以正確序列化/反序列化。當(dāng)我直接在 redis 緩存中插入 JSON 時(shí),我無(wú)法使用此序列化程序?qū)ζ溥M(jìn)行反序列化,序列化程序只會(huì)給我一個(gè) Person 對(duì)象,其名稱、電子郵件和 id 屬性為空。有沒(méi)有辦法來(lái)解決這個(gè)問(wèn)題?如果有辦法改善我的問(wèn)題,請(qǐng)告訴我。
查看完整描述

3 回答

?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

GenericJackson2JsonRedisSerializer假設(shè)杰克遜的默認(rèn)類型。當(dāng)您GenericJackson2JsonRedisSerializer使用ObjectMapper實(shí)例創(chuàng)建時(shí),請(qǐng)確保配置默認(rèn)類型 ( enableDefaultTyping(…))。

默認(rèn)類型最適合非最終類型,并且需要在所有 JSON 有效負(fù)載中為類型提供一致的屬性名稱,以便 Jackson 可以識(shí)別要反序列化的適當(dāng)類型。

默認(rèn)類型使用動(dòng)態(tài)類型標(biāo)記,如果您的數(shù)據(jù)源(Redis 實(shí)例)不完全受信任,那么這可能會(huì)成為安全問(wèn)題。

Jackson2JsonRedisSerializer固定到特定類型并消除動(dòng)態(tài)類型風(fēng)險(xiǎn)。


查看完整回答
反對(duì) 回復(fù) 2022-07-27
?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以使用 Spring Data Redis


添加依賴項(xiàng):


<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

啟用緩存和使用 Jackson2JsonRedisSerializer


package com.redis.demo.redisdemo.config;


import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CachingConfigurerSupport;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.cache.RedisCacheConfiguration;

import org.springframework.data.redis.cache.RedisCacheManager;

import org.springframework.data.redis.cache.RedisCacheWriter;

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.RedisSerializationContext;

import org.springframework.data.redis.serializer.StringRedisSerializer;


@EnableCaching

@Configuration

public class RedisConfig extends CachingConfigurerSupport {

    @Autowired

    private RedisConnectionFactory redisConnectionFactory;


    @Bean

    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        serializer.setObjectMapper(objectMapper);

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(redisConnectionFactory);

        redisTemplate.setKeySerializer(new StringRedisSerializer());

        redisTemplate.setValueSerializer(serializer);

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        redisTemplate.setHashValueSerializer(serializer);

        redisTemplate.afterPropertiesSet();

        return redisTemplate;

    }


    @Bean

    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {

        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()

                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));

        return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);

    }

}

在方法中添加可緩存注釋以緩存在redis中


@Cacheable(value = "employee", key = "#id")

    public Employee getEmployee(Integer id) {

        log.info("Get Employee By Id: {}", id);

        Optional<Employee> employeeOptional = employeeRepository.findById(id);

        if (!employeeOptional.isPresent()) {

            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Id Not foud");

        }

        return employeeOptional.get();

    }


查看完整回答
反對(duì) 回復(fù) 2022-07-27
?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

我知道這個(gè)問(wèn)題被問(wèn)到已經(jīng)很久了,但可能還有人需要答案。

我有同樣的問(wèn)題,我使用JdkSerializationRedisSerializer而不是解決它GenericJackson2JsonRedisSerializer。

我的RedisCacheConfiguration豆子看起來(lái)像:

return RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofHours("your-long-value"))
            .serializeKeysWith(SerializationPair.fromSerializer(new StringRedisSerializer()))
            .serializeValuesWith(SerializationPair.fromSerializer(new JdkSerializationRedisSerializer()));



查看完整回答
反對(duì) 回復(fù) 2022-07-27
  • 3 回答
  • 0 關(guān)注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)