title: Custom @Id class for Spring Data Redis
body: <p>I am trying to convert / serialize the @Id field (which is not a string) of the model class but keep getting this error. The custom ID class just concatenates two values with a colon, e.g. <code>aaaa:2345</code>.</p>
  
  <pre><code>org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [MyIdClass] to type [byte[]]
  </code></pre>
  
  <p>This is my model class.</p>
  
  <pre><code>@RedisHash("alert")
  public class MyClass implements Serializable
  {
      public static class MyIdClass
      {
          public String userId;
          public Long sessionExpiry;
  
          public MyIdClass()
          {
          }
  
          public MyIdClass(String id, Long ex)
          {
              userId = id;
              sessionExpiry = ex;
          }
      }
  
      public static class MyIdClassSerializer implements RedisSerializer&lt;MyIdClass&gt;
      {
          @Nullable
          @Override
          public byte[] serialize(@Nullable MyIdClass uid) throws SerializationException
          {
              return String.format("%s:%d", uid.userId, uid.sessionExpiry).getBytes();
          }
  
          @Nullable
          @Override
          public MyIdClass deserialize(@Nullable byte[] bytes) throws SerializationException
          {
              String[] t = new String(bytes).split(":");
              return new MyIdClass(t[0], Long.parseLong(t[1]));
          }
      }
  
      @Component
      @ReadingConverter
      public static class MyIdClassReader implements Converter&lt;byte[], MyIdClass&gt;
      {
  
          @Nullable
          @Override
          public MyIdClass convert(byte[] source)
          {
              String[] t = new String(source).split(":");
              return new MyIdClass(t[0], Long.parseLong(t[1]));
          }
      }
  
      @Component
      @WritingConverter
      public static class MyIdClassWriter implements Converter&lt;MyIdClass, byte[]&gt;
      {
  
          @Nullable
          @Override
          public byte[] convert(MyIdClass uid)
          {
              return String.format("%s:%d", uid.userId, uid.sessionExpiry).getBytes();
          }
      }
  
      /**
       * User ID
       * Key := 'alert:' userId ':' sessionExpiry
       */
      @Id
      public MyIdClass id;
  
      public String value;
  }
  </code></pre>
  
  <p>Here's how I'm configuring the RedisTemplate.</p>
  
  <pre><code>@Bean("redisTemplateActivityAlert")
  public RedisTemplate&lt;ActivityAlert.UserIdExpiry, ActivityAlert&gt; redisTemplateActivityAlert()
  {
      RedisTemplate&lt;ActivityAlert.UserIdExpiry, ActivityAlert&gt; template = new RedisTemplate&lt;&gt;();
      template.setConnectionFactory(jedisConnectionFactory());
      template.setKeySerializer(new ActivityAlert.UserIdExpirySerializer());
      template.setHashKeySerializer(new ActivityAlert.UserIdExpirySerializer());
      return template;
  }
  </code></pre>
  
  <p>I've read other posts about configuring <code>ConversionService</code> or <code>TypeConverter</code> but haven't gone far with them.</p>