鍍金池/ 問(wèn)答/人工智能  Java/ Redis Template<String, User> Excep

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.

回答
編輯回答
半心人

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

類(lèi)型轉(zhuǎn)換錯(cuò)誤

2018年7月2日 08:35