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

How to solve Redis serialization conversion type error

WBOY
Release: 2023-05-30 20:26:00
forward
1291 people have browsed it

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());
Copy after login

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));
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template