Redis Template<String, User> Exception

My code like this:

Java:

@Autowired
private RedisTemplate<String,User> myTemplate;

@Override
public String login(String email, String password) {
    User user = this.userRepository.findByEmailAndPassword(email, password);

    System.out.println(user);

    if (user == null)  return null;

    String key1 = "lic" + "$" + user.getId() + "$" + user.getRole() + "$" + user.getName() + "$" + user.getEmail();

    ValueOperations<String, User> ops = this.myTemplate.opsForValue();

    if (!this.myTemplate.hasKey(key1)) {
        ops.set(key1, user);
    }
    return key1;
}
when app run, inject bean ,like this:

@SpringBootApplication
public class ApplicationApp extends WebMvcConfigurerAdapter {


//    @Autowired
//    private RedisTemplate<String,String> template;

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
}

@Bean
RedisTemplate<String, User> redisTemplate() {
    final RedisTemplate<String, User> template = new RedisTemplate<String, User>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericToStringSerializer<User>(User.class));
    template.setValueSerializer(new GenericToStringSerializer<User>(User.class));
    return template;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    // super.addInterceptors(registry);
    registry.addInterceptor(new AuthorAccess()).addPathPatterns("/api/sc/**");
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(ApplicationApp.class, args);
}
}

then , call login service found error like this:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type com.qycloud.oatos.license.domain.User to type java.lang.String

Trying to implement as I got resources from internet, but not solved.

阅读 1.8k
1 个回答

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type com.qycloud.oatos.license.domain.User to type java.lang.String

类型转换错误

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题