title: Java Cannot cast reactor.core.publisher.MonoDefer to EntityClass
body: <p>Here i try to upgrade my spring-data-redis <code>RedisTemplate</code> using reactive-redis <code>ReactiveRedisTemplate</code> which is returning Publisher. In this case, i want to change the method <code>findCache</code> to <code>Mono</code>. The problem is old <code>findCache</code> function that using spring-data-redis is accepting generic data, like this:</p>
  
  <pre><code>@Autowired
  ReactiveRedisTemplate redisTemplate;
  
  public &lt;T&gt; T findCache(String key, Class&lt;T&gt; clazz) {
      Object content = this.redisTemplate.opsForValue().get(key);
  
      if (content != null) {
        return clazz.cast(content);
      }
  
      return null;
    }
  </code></pre>
  
  <p>of course i will get error </p>
  
  <pre><code>Cannot cast reactor.core.publisher.MonoDefer to Person
  </code></pre>
  
  <p>then, since i want to get it to work reactively i update this code to return publisher, like this:</p>
  
  <pre><code>if (content != null) {
        return ((Mono) content).flatMap(o -&gt; clazz.cast(o));
      }
  </code></pre>
  
  <p>but it also won't work since my <code>findCache</code> accepting generic.
  what supposed i need to do, please help.</p>