You need to enable JavaScript to run this app. Spring Data Redis使用UUID作为实体ID时出现ConverterNotFoundException
优惠活动
大模型
产品
解决方案
定价
更多

Spring Data Redis使用UUID作为实体ID时出现ConverterNotFoundException

解决Spring Data Redis中UUID作为主键的ConverterNotFoundException问题

这个问题我之前在项目里也碰到过,核心原因很明确:Spring Data Redis默认没有提供UUID与byte[]之间的转换逻辑——因为Redis底层存储的是二进制数据,当你用UUID作为@Id字段时,框架找不到合适的转换器来完成UUID到byte[]的转换,就会抛出这个异常。

下面给你两种实用的解决方案,你可以根据自己的需求选择:


方案一:自定义UUID与byte[]的转换器(二进制存储)

这种方案会把UUID转换成16字节的二进制数据存储,比较节省Redis的存储空间。

1. 实现转换器类

首先编写两个转换器,分别处理UUID转byte[]和byte[]转UUID的逻辑:

import java.nio.ByteBuffer;
import java.util.UUID;
import org.springframework.core.convert.converter.Converter;

// UUID 转 byte[]
public class UUIDToBytesConverter implements Converter<UUID, byte[]> {
    @Override
    public byte[] convert(UUID source) {
        byte[] bytes = new byte[16];
        ByteBuffer.wrap(bytes)
                .putLong(source.getMostSignificantBits())
                .putLong(source.getLeastSignificantBits());
        return bytes;
    }
}

// byte[] 转 UUID
public class BytesToUUIDConverter implements Converter<byte[], UUID> {
    @Override
    public UUID convert(byte[] source) {
        ByteBuffer buffer = ByteBuffer.wrap(source);
        long mostSignificantBits = buffer.getLong();
        long leastSignificantBits = buffer.getLong();
        return new UUID(mostSignificantBits, leastSignificantBits);
    }
}

2. 注册转换器到Spring容器

创建Redis配置类,把自定义转换器注册到RedisCustomConversions中:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.convert.RedisCustomConversions;
import java.util.Arrays;

@Configuration
public class RedisConfig {

    @Bean
    public RedisCustomConversions redisCustomConversions() {
        return new RedisCustomConversions(Arrays.asList(
                new UUIDToBytesConverter(),
                new BytesToUUIDConverter()
        ));
    }
}

这样Spring Data Redis就会自动使用这些转换器来处理UUID类型的主键了。


方案二:将UUID转为String存储(更简单易读)

如果对存储空间要求不高,或者希望Redis中的主键可读性更好(方便调试),可以直接把UUID转成字符串存储,Spring Data Redis默认支持String与byte[]的转换,不需要额外写复杂的二进制转换逻辑。

方式1:直接修改实体类字段类型

把@Id字段改成String类型,在设置ID时自动把UUID转成字符串:

import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

@RedisHash("order")
public class Order {
    @Id
    private String id;

    // 无参构造时自动生成UUID字符串
    public Order() {
        this.id = UUID.randomUUID().toString();
    }

    // 提供UUID类型的getter,方便业务层使用
    public UUID getUuidId() {
        return UUID.fromString(this.id);
    }

    // 如果需要手动设置ID,支持传入UUID
    public void setId(UUID id) {
        this.id = id.toString();
    }

    // ...其他业务字段
}

方式2:用@Convert注解自动转换

如果不想修改字段类型,也可以用@Convert注解指定转换器,让框架自动完成UUID和String的转换:

import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.Convert;
import org.springframework.data.redis.core.RedisHash;

@RedisHash("order")
public class Order {
    @Id
    @Convert(converter = UUIDToStringConverter.class)
    private UUID id;

    // ...其他业务字段
}

// 实现UUID转String的转换器
class UUIDToStringConverter implements Converter<UUID, String> {
    @Override
    public String convert(UUID source) {
        return source.toString();
    }
}

// 实现String转UUID的转换器
class StringToUUIDConverter implements Converter<String, UUID> {
    @Override
    public UUID convert(String source) {
        return UUID.fromString(source);
    }
}

同样,需要把这两个转换器注册到RedisCustomConversions中(参考方案一的配置类)。


两种方案各有优劣:

  • 方案一:二进制存储更节省空间,适合对Redis存储容量敏感的场景;
  • 方案二:字符串存储可读性强,调试更方便,实现成本更低。

你可以根据自己的项目需求选择合适的方案~

内容的提问来源于stack exchange,提问作者nagy.zsolt.hun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:19:35