一般来说:我从kafka流中读取序列化对象(作为json),并尝试使用spring数据存储库将其保存到redis。
在对repository.save()进行了两次调用(对象尚未保存到redis)之后,我得到了StackOverflower错误:
Exception in thread "processOffers-applicationId-1c24ef63-baae-47b9-beb7-5e6517736bc4-StreamThread-1" java.lang.StackOverflowError
at org.springframework.data.util.Lazy.get(Lazy.java:94)
at org.springframework.data.mapping.model.AnnotationBasedPersistentProperty.usePropertyAccess(AnnotationBasedPersistentProperty.java:277)
at org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:134)
at org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:115)
at org.springframework.data.redis.core.convert.MappingRedisConverter.lambda$writeInternal$2(MappingRedisConverter.java:601)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:353)
at org.springframework.data.redis.core.convert.MappingRedisConverter.writeInternal(MappingRedisConverter.java:597)
at org.springframework.data.redis.core.convert.MappingRedisConverter.lambda$writeInternal$2(MappingRedisConverter.java:639)
序列化的pojo如下所示:
@Data
@With
@NoArgsConstructor
@AllArgsConstructor
@RedisHash("students")
public class Student {
@Id
@JsonProperty("student_id")
private long id;
@JsonProperty("entities")
private Map<String, Object> entities = new HashMap<>();
}
Map实体包含100多个条目,带有嵌套的Map(对象)。
有趣的是:如果我把Map清空,一切都正常,数据会立即保存到redis。
pojo的对应存储库:
@Repository
public interface StudentRepository extends CrudRepository<Student, Long> {
}
另外,我还为长id字段定义了rediscustomconversion:
@Component
@ReadingConverter
public class BytesToLongConverter implements Converter<byte[], Long> {
@Override
public Long convert(final byte[] source) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(source);
buffer.flip();
return buffer.getLong();
}
}
@Component
@WritingConverter
public class LongToBytesConverter implements Converter<Long, byte[]> {
@Override
public byte[] convert(final Long source) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(source);
return buffer.array();
}
}
redis配置类如下所示:
@Configuration
@EnableRedisRepositories
public class RedisConfiguration {
@Bean
@Primary
public RedisProperties redisProperties() {
return new RedisProperties();
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
var config = new RedisStandaloneConfiguration();
var props = redisProperties();
config.setHostName(props.getHost());
config.setPort(props.getPort());
return new JedisConnectionFactory(config);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
var template = new RedisTemplate<String, Object>();
template.setConnectionFactory(redisConnectionFactory());
template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
@Bean
public RedisCustomConversions redisCustomConversions(LongToBytesConverter longToBytes,
BytesToLongConverter bytesToLong) {
return new RedisCustomConversions(Arrays.asList(longToBytes, bytesToLong));
}
}
upd:我在spring数据redis jira上发现了这个问题,但是分辨率设置为“fixed”,所以我觉得很奇怪。
1条答案
按热度按时间ccrfmcuu1#
我已经使用genericjackson2jsonredisserializer为pojo中的内部Map定义了自定义writingconverter和readingconverter,一切都解决了!代码: