【问题标题】:Converting HeapByteBuffer to UUID将 HeapByteBuffer 转换为 UUID
【发布时间】:2019-10-07 08:51:29
【问题描述】:

我的应用程序基于 Spring 反应式 mysql。当我尝试从 mysql db 获取数据时,出现错误“org.springframework.core.convert.ConverterNotFoundException:找不到能够从类型 [java.nio.HeapByteBuffer] 转换为类型 [java.util.UUID] 的转换器”

我尝试将转换器添加到实现 WebFluxConfigurer 的 WebConfig,但是当 Spring 查找转换器时,由于 ByteBuffer 而不是 HeapByteBuffer(此类受保护,扩展 ByteBuffer)而无法找到。

@Configuration
@EnableWebFlux
public class WebConfig  implements WebFluxConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new ByteBufferToUUIDConverter());
    }
}

和转换的伪代码

public class ByteBufferToUUIDConverter implements Converter<ByteBuffer, UUID>, ConditionalConverter {
    private Logger logger = LogManager.getLogger(ByteBufferToUUIDConverter.class);

    @Override
    public UUID convert(ByteBuffer source) {
        return null;
    }

    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        logger.debug(":TypeDescriptor " + sourceType + ", " + targetType);
        return true;
    }
}

【问题讨论】:

    标签: spring converters


    【解决方案1】:

    在尝试包装 ByteBuffer 及其子类后,我找到了一个更简单的解决方案。

    1. @Entity 中的UUID 更改为byte[]
    2. 删除无用的类。

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2011-03-28
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多