如何解决Spring Data Redis 组合键
我需要在 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 举报,一经查实,本站将立刻删除。