How to Store non-serialized object into rediscache using kryoSerialzation

I’m using redis cache on my spring boot application and i need to store an unknown type of response object into redisTemplate. so i’m using kryoSerialzation on my redisTemplates,

Exception :

org.objenesis.ObjenesisException: class not serializable
at org.objenesis.strategy.SerializingInstantiatorStrategy.newInstantiatorOf(
at com.esotericsoftware.kryo.Kryo.newInstantiator(
at com.esotericsoftware.kryo.Kryo.newInstance(


I couldn’t implements Serializable interface on my response object, since its dynamic and unknown type on my application



    public class KryoRedisSerializer implements RedisSerializer<Object>{

    private  final ThreadLocal<Kryo> kryoThreadLocal = new ThreadLocal<Kryo>() {
         protected Kryo initialValue() {
             Kryo kryo = new Kryo();
             return kryo;
     public KryoRedisSerializer() {
         kryoThreadLocal.get().setInstantiatorStrategy((InstantiatorStrategy) new SerializingInstantiatorStrategy());

    public byte[] serialize(Object object) throws SerializationException {
        // TODO Auto-generated method stub
         if (object == null) {
             return new byte[0];

          try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
             Output output = new Output(outputStream);
             kryoThreadLocal.get().writeClassAndObject(output, object);
             return output.toBytes();
          } catch (IOException e) {
             throw new SerializationException("Failed Serialization", e);

    public Object deserialize(byte[] bytes) throws SerializationException {
        // TODO Auto-generated method stub
          if (bytes == null || bytes.length == 0) {
             return null;
          try (Input input = new Input(bytes)) {
             return kryoThreadLocal.get().readClassAndObject(input);

public class SpringRadisConfig {
    public JedisConnectionFactory connectionFactory() {
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        return connectionFactory;

    public RedisTemplate<String, Object> redisTemplate() {

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

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());


        return redisTemplate;
    public RedisSerializer<Object> kryoRedisEOSSerializer() {
       return new KryoRedisEOSSerializer();
} (main method)

@ComponentScan(basePackages = "")
public class SampleRedisApplication implements CommandLineRunner {

    private StringRedisTemplate template;

    private SpringRadisConfig redisConfig;

    public void run(String... args) throws Exception {/*
        ValueOperations<String, String> ops = this.template.opsForValue();
        String key = "spring.boot.redis.test";
        if (!this.template.hasKey(key)) {
            ops.set(key, "Hi Ratheesh ");
        System.out.println("Found key " + key + ", value=" + ops.get(key));*/

        RedisTemplate<String, Object> redisTemplate = redisConfig.redisTemplate();
        ValueOperations<String, Object> values = redisTemplate.opsForValue();

        try { 
            Student student = new Student(1L, "Vishal");
            values.set("student", student);
            Student std = (Student) values.get("student");


        } catch(Exception e) {

    public static void main(String[] args) throws Exception {
        // Close the context so it doesn't stay awake listening for redis, args).close();


Assume student is the response object which i’m getting from remote API, actually i don’t have any model object definition(POJO) on my application for API response, because my API response type is (Type T) ie dynamic nodes

I found a solution in github but that is not working (convert object to array and store it Redis)