spring仓库findById()在spring-data-redis 2.3.1.RELEASE版本上正常工作
它在spring-data-redis版本2.3.2上失败了
这里是示例存储库的链接,在pom.xml文件中编辑版本,然后运行,然后查看问题
https://github.com/mnguyencntt/spring-data-redis-optional
我的逻辑代码很简单:
如果找到了StudentId,则返回现有的RedisStudent对象。
否则,创建新的RedisStudent并将其存储在Redis中,返回新的RedisStudent对象。
RedisInfoController.java
final Optional<RedisStudent> redisExisting = redisStudentRepository.findById(studentId);
if (redisExisting.isPresent()) {
// Spring boot 2.3.2 will print out: RedisStudent(id=null, name=null, age=null, creationTime=null)
// Spring boot 2.3.1 will print out: RedisStudent(id=12345, name=Minh, age=28, creationTime=2020-07-28T21:31:18.318)
log.info("{}", redisExisting.get());
return redisExisting.get();
}
// Spring boot 2.3.1 will print out: Optional.empty
log.info("{}", redisExisting);
RedisStudent student = new RedisStudent();
student.setId(studentId);
student.setName("Minh");
student.setAge("28");
student.setCreationTime(LocalDateTime.now());
return redisStudentRepository.save(student);
答案 0 :(得分:2)
答案 1 :(得分:-2)
也许是因为您的控制器中的StudentId为空?
您没有在请求参数中使用StudentId。