-1

I am going to store my token in redis

RedisConfig

@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("127.0.0.1");
        redisStandaloneConfiguration.setPort(6379);


        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate() {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

Service

final var st =AuthenticationSuccessDto
                .builder()
                .accessToken(jwtUtils.generateAccessToken(user))
                .refreshToken(jwtUtils.generateRefreshToken(user))
                .tokenType("Bearer")
                .user(user)
                .expiresIn(TimeUnit.SECONDS.toSeconds(60)).build();

        try {
            redisTemplate.opsForHash().put(KEY,"1",st.getAccessToken());
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception();
        }

        return st;
    }

throws an exception java.lang.NoSuchMethodError: 'long redis.clients.jedis.Jedis.hset(byte[], byte[], byte[])'

I need to convert string to bytes? help plz

Borka192
  • 23
  • 4
  • Does this answer your question? [Convert bytes to a string](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – storenth Dec 14 '22 at 15:48
  • StringRedisSerializer and JdkSerializationRedisSerializer perform this transformation, everything worked for me when I changed the connection to the redis from JedisConnectionFactory to LettuceConnectionFactory – Borka192 Dec 15 '22 at 03:18

2 Answers2

0

You can also have Spring Boot automatically generate the connection to Redis based on your .yml or a .properties file. You just need to add the following to your application.yml:

spring:
  redis:
    host: localhost
    port : '6379'
nakhodkin
  • 1,327
  • 1
  • 17
  • 27
0

Everything worked for me when I changed the connection to the redis from JedisConnectionFactory to LettuceConnectionFactory

@Bean
    public LettuceConnectionFactory redisStandAloneConnectionFactory() {
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379));
    }
Borka192
  • 23
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 18 '22 at 06:23