Redis-springboot-redistemplate changing serialization mode

Source: Internet
Author: User
Tags propertyaccessor redis serialization

Redistemplate default serialization mode is Jdkserializeable, stringredistemplate default serialization is Stringredisserializer

You can change the serialization of redistemplate by manually configuring it

Package Com.wenbronk.data.redis;
Import Com.fasterxml.jackson.annotation.JsonAutoDetect;
Import Com.fasterxml.jackson.annotation.PropertyAccessor;
Import Com.fasterxml.jackson.databind.ObjectMapper;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.context.annotation.Bean;
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;
 /** * Redis Startup class * Created by Wenbronk on 2017/6/12. */@SpringBootApplication public class Redisapplication {public static void main (string[] args) {springappli
    Cation.run (Redisapplication.class, args); }/** * Redistemplate serialization using jdkserializeable, storing binary bytecode, so custom serialization class * @param redisconnectionfactory * @r ETurn */@Bean public redistemplate<object, object> redistemplate (redisconnectionfactory redisconnection
        Factory) {redistemplate<object, object> redistemplate = new redistemplate<> ();

        Redistemplate.setconnectionfactory (redisconnectionfactory); Replace the default serialization Jackson2jsonredisserializer Jackson2jsonredisserializer = new Jackson2Json using Jackson2jsonredisserialize

        Redisserializer (Object.class);
        Objectmapper objectmapper = new Objectmapper ();
        Objectmapper.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY);

        Objectmapper.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);

        Jackson2jsonredisserializer.setobjectmapper (Objectmapper);
        Sets the serialization rule for value and the serialization rule for key Redistemplate.setvalueserializer (Jackson2jsonredisserializer);
        Redistemplate.setkeyserializer (New Stringredisserializer ());
        Redistemplate.afterpropertiesset ();
   return redistemplate; }
} 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.