问题描述
我需要在 Redis 中存储以下类:
@Data
@Builder
@Accessors(chain = true)
@RedisHash("Meter")
public class Meter {
@org.springframework.data.annotation.Id
private Id id;
private String accountId;
private String name;
private Tags tags;
private Type type;
@Getter
@requiredArgsConstructor
public static class Id {
private final String name;
private final String accountId;
@Override
public String toString() {
return name + ":" + accountId;
}
public static Id of(String name,String accountId) {
return new Id(name,accountId);
}
}
}
如你所见,它有一个自定义的 id 类。这个想法是这个自定义键类将在 Redis 中表示为 name:accountId。我也有这个类的存储库:
public interface MeterRepository extends CrudRepository<Meter,Meter.Id> {
}
当我运行项目并尝试使用 MeterRepository 时,我得到了 No converter found capable of converting from type [...entity.Meter$Id] to type [java.lang.String]
,尽管我创建了一个转换器并注册了它:
@Component
public class MeterIdToStringConverter implements Converter<Meter.Id,String> {
@Override
public String convert(Meter.Id meterId) {
return meterId.toString();
}
}
@Configuration
@requiredArgsConstructor
public class ConversionConfiguration {
private final MeterIdToStringConverter meterIdToStringConverter;
@Bean
public ConversionService conversionService(List<Converter> converters) {
final DefaultConversionService conversionService = new DefaultConversionService();
converters.forEach(conversionService::addConverter);
conversionService.addConverter(meterIdToStringConverter);
return conversionService;
}
}
此外,我尝试自动装配 ConversionService 并调用 conversionService.canConvert(Meter.Id.class,String.class)
- 它返回 true
。
我做错了什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)