Spring Data Redis - UUID id - ConverterNotFoundException

时间:2018-04-17 00:13:51

标签: spring spring-boot redis spring-data uuid

我想使用UUID密钥将我的实体存储在Redis中:

@RedisHash("order")
public class Order {

    @Id
    private UUID id;

    ...
}

但是,我通过此设置获得以下异常:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.UUID] to type [byte[]]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.toBytes(MappingRedisConverter.java:948) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.lambda$writeInternal$2(MappingRedisConverter.java:592) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:328) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.writeInternal(MappingRedisConverter.java:584) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:396) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.convert.MappingRedisConverter.write(MappingRedisConverter.java:122) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.redis.core.RedisKeyValueAdapter.put(RedisKeyValueAdapter.java:208) ~[spring-data-redis-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.lambda$update$1(KeyValueTemplate.java:204) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:343) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.core.KeyValueTemplate.update(KeyValueTemplate.java:204) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository.save(SimpleKeyValueRepository.java:103) ~[spring-data-keyvalue-2.0.6.RELEASE.jar:2.0.6.RELEASE]
...

使用String而不是UUID时,不会出现此类异常。

如何将UUID用作ID类型?

1 个答案:

答案 0 :(得分:1)

编辑:

更准确地说,您必须在上下文中使用名称redisCustomConversions注册CustomConversion bean。请参阅此帖子:Redis - How to configure custom conversions

所以在你的情况下,它会像

@Bean
public CustomConversion redisCustomConversions(){
    return new CustomConversions(
        Arrays.asList(new UUIDToStringConverter(), new StringToUUIDConverter()))))
}

原件:

我认为解决这个问题的最简单方法是为uuid编写一个类型转换器。 像

这样的东西

class UUIDConverter implements Converter<UUID, String>

或在需要时

class UUIDConverter implements Converter<UUID, byte[]>

您必须在上下文中将其注册为bean。