Skip to content

MappingRedisConverter does not support generic properties #2349

Closed
@renjiancheng

Description

@renjiancheng

Sorry, the problem on StackOverflow didn't answer, so I moved it over here, in spring-data-redis, I added a piece of data with generics using stream and Jackson2HashMapper, but when a consumer consumes it, it can only read the entity's type. All other attributes are lost. How can I consume it correctly?
producer code:

    @Bean
    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rf) {
        RedisTemplate<String, Object> rt = new RedisTemplate<>();
        rt.setConnectionFactory(rf);
        rt.setKeySerializer(RedisSerializer.string());
        rt.setValueSerializer(RedisSerializer.json());
        rt.setHashKeySerializer(RedisSerializer.string());
        rt.setHashValueSerializer(RedisSerializer.json());
        return rt;
    }

    @Bean
    ApplicationRunner runner(RedisTemplate<String, Object> rt) {
        return arg -> {
            MyMessage<User> userMyMessage = new MyMessage<User>().setReceiver(1)
                                                                 .setClientType("app")
                                                                 .setPayload(new User().setName("testName")
                                                                                       .setAge(10)
                                                                                       .setId(1));
            Jackson2HashMapper hashMapper = new Jackson2HashMapper(false);
            rt.opsForStream()
              .add(StreamRecords.newRecord()
                                .in("my-stream")
                                .ofMap(hashMapper.toHash(userMyMessage)));
        };
    }

sequence in Redis:
image
consumer code:

 @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rcf) {
        RedisTemplate<String, Object> rt = new RedisTemplate<>();
        rt.setConnectionFactory(rcf);
        rt.setKeySerializer(RedisSerializer.string());
        rt.setValueSerializer(RedisSerializer.json());
        rt.setHashKeySerializer(RedisSerializer.string());
        rt.setHashValueSerializer(RedisSerializer.json());
        return rt;
    }

    @Bean
    StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, ObjectRecord<String, Object>> hashContainerOptions() {
        return StreamMessageListenerContainer.StreamMessageListenerContainerOptions.builder()
                                                                                   .pollTimeout(Duration.ofSeconds(1))
                                                                                   .objectMapper(new Jackson2HashMapper(false))
                                                                                   .build();
    }

    @Bean
    StreamMessageListenerContainer<String, ObjectRecord<String, Object>> hashContainer(
            StreamMessageListenerContainer.StreamMessageListenerContainerOptions<String, ObjectRecord<String, Object>> options,
            RedisConnectionFactory redisConnectionFactory,
            RedisTemplate<String, Object> redisTemplate) {

        var container = StreamMessageListenerContainer.create(redisConnectionFactory, options);
        container.start();
        String group = "default-group";
        String key = "my-stream";
        try {
            redisTemplate.opsForStream().createGroup(key, group);
        } catch (Exception ignore) {
        }
        container.receiveAutoAck(
                Consumer.from(group, "default-consumer"),
                StreamOffset.create(key, ReadOffset.lastConsumed()),
                message -> {
                    log.info("receive message stream:{}, id:{} value:{}", message.getStream(), message.getId(), message.getValue());
                }
        );
        return container;
    }

consumer log:

receive message stream:my-stream, id:1655890663894-0 value:"com.commons.MyMessage"

thanks

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions