Home  >  Article  >  Database  >  How to solve Redis serialization conversion type error

How to solve Redis serialization conversion type error

WBOY
WBOYforward
2023-05-30 20:26:001190browse

Cannot convert value of type 'org.springframework.data.redis.core.convert.MappingRedisConverter' to required type 'org.springframework.data.redis.core.mapping.RedisMappingContext': no ​​matching editors or conversion strategy found

An error was reported during the serialization method of setValue. The RedisSerializer.json() method was originally used, but an error was reported.

template.setConnectionFactory(factory);
        // key序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // value序列化方式
        template.setValueSerializer(RedisSerializer.json());
        // hash key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // hash value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());

Changed to the following to solve the problem:

template.setConnectionFactory(factory);
        // key序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // value序列化方式
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        // hash key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // hash value的序列化方式
        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));

In Redis, there are multiple serialization implementations to choose from. Among them, Jackson2JsonRedisSerializer and RedisSerializer.json() are serialization implementations in Redis. Their differences are as follows:

Jackson2JsonRedisSerializer uses the Jackson library to serialize Java objects into JSON-formatted strings and store them into Redis. It can also convert JSON strings read from Redis into Java objects. Therefore, using Jackson2JsonRedisSerializer can easily process JSON format data, and can perform better serialization and deserialization of complex Java objects. To fully control the serialization process, certain configuration is required because some fields in Java objects may be ignored during serialization.

RedisSerializer.json() uses Redis's built-in JSON serializer to serialize Java objects into JSON-formatted strings and store them in Redis. It can also be used to deserialize JSON strings taken from Redis into Java objects. Compared with Jackson2JsonRedisSerializer, RedisSerializer.json() will serialize all fields in Java objects to Redis during the serialization process, but when dealing with complex Java objects, some additional configuration may be required.

Therefore, which serialization method to choose depends on the usage scenario and specific needs. If you need to deal with complex Java objects and fine control over serialization and deserialization is critical, then Jackson2JsonRedisSerializer is a better choice. And if the data being processed is relatively simple, or you only need to simply convert Java objects into JSON format strings for storage, then RedisSerializer.json() may be more suitable.

The above is the detailed content of How to solve Redis serialization conversion type error. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete