小编典典

Spring Data Redis-UUID ID-ConverterNotFoundException

redis

我想使用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类型?


阅读 632

收藏
2020-06-20

共1个答案

小编典典

编辑:

为了更精确,您必须在上下文中使用名称redisCustomConversions注册一个CustomConversion
bean。

所以在你的情况下

@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。

2020-06-20