我正在尝试将CrudRepository与spring-data-redis和生菜结合使用。遵循所有建议,我发现已使用@ReadingConverters和@WritingConverters配置了spring-boot 2.1.8应用程序,但是当我尝试使用存储库时,我得到“属性的路径不能为null或为空。”
进行一些调试,这似乎是由org.springframework.data.redis.core.convert.MappingRedisConverter:393
writeInternal(entity.getKeySpace(), "", source, entity.getTypeInformation(), sink);
第二个参数是路径。结束于运行以下代码的MappingRedisConverter
的第747行:
} else if (targetType.filter(it -> ClassUtils.isAssignable(byte[].class, it)).isPresent()) {
sink.getBucket().put(path, toBytes(value));
}
最终,具有空路径的认沽权最终以org.springframework.data.redis.core.convert.Bucket:77
结束,并且Assert.hasText(path, "Path to property must not be null or empty.");
失败,即使数据已被序列化。
这是spring-data-redis的错误,还是我需要配置其他东西?
RedicsConfig.java
@Configuration
@EnableConfigurationProperties({RedisProperties.class})
@RequiredArgsConstructor
@EnableRedisRepositories
public class RedisConfiguration {
private final RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
template.setConnectionFactory(redisConnectionFactory);
template.afterPropertiesSet();
return template;
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.findAndRegisterModules();
return objectMapper;
}
@Bean
public RedisCustomConversions redisCustomConversions(List<Converter<?,?>> converters) {
return new RedisCustomConversions(converters);
}
}
我这里只包括一个写转换器,但是有几个读和写转换器...
@Component
@WritingConverter
@RequiredArgsConstructor
@Slf4j
public class CategoryWritingConverter implements Converter<Category, byte[]> {
private final ObjectMapper objectMapper;
@Setter
private Jackson2JsonRedisSerializer<Category> serializer;
@Override
public byte[] convert(Category category) {
return getSerializer().serialize(category);
}
private Jackson2JsonRedisSerializer<Category> getSerializer() {
if (serializer == null) {
serializer = new Jackson2JsonRedisSerializer<>(Category.class);
serializer.setObjectMapper(objectMapper);
}
return serializer;
}
}
要写入的对象:
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("category")
@TypeAlias("category")
public class Category {
@Id
@EqualsAndHashCode.Include
private String categoryCode;
private String categoryText;
}
和回购:
public interface CategoryRepository extends CrudRepository<Category, String> {
Page<Category> findAll(Pageable pageable);
}
有人可以告诉我我错过了什么吗,或者这是我应该在spring-data-redis上引发的错误吗?