【发布时间】:2022-01-18 22:48:27
【问题描述】:
我正在开发一个 Spring Boot 应用程序,并且我正在尝试使用更现代的(Java 8+ 构造),但我发现尝试对现有代码实施以下更改时遇到了一些困难。
我有如下服务方式:
@Override
public Optional<User> activateDeactivateUser(int userId, boolean isActive) throws NotFoundException {
Optional<User> retrievedUser = this.userRepository.findById(userId);
if(retrievedUser.isEmpty())
throw new NotFoundException(String.format("The user having ID or email %s was not found", userId));
return Optional.ofNullable(retrievedUser)
.map((user) -> {
log.info(String.format("****** USER E-MAIL *******", user.get().getEmail()));
user.get().set_active(isActive);
this.userRepository.save(user.get());
return user;
})
.orElseThrow(() -> new NotFoundException(String.format("The user having ID or email %s was not found", userId)));
}
如您所见,此方法返回一个 可选 对象。首先,我不确定返回 Optional 是否是一种好习惯(因为如果它为空,我将抛出并处理 NotFoundException)。
但主要问题是我的 User 类是一个实体类(我正在使用 Hibernate,因此它包含表映射)我想更改以前的方法以检索 UserDTO 对象。
为了将 User 实例转换为 UserDTO 实例,我将这个 ConversionService 注入到我的服务类中(我在其他地方使用过)
@Autowired
ConversionService conversionService;
我的主要问题是,在我以前的方法中,我使用的是 map() 运算符。我曾尝试用这种方式改变之前的服务方式:
@Override
public UserDTO activateDeactivateUser(int userId, boolean isActive) throws NotFoundException {
Optional<User> retrievedUser = this.userRepository.findById(userId);
User userToBeUpdated = null;
if(retrievedUser.isEmpty())
throw new NotFoundException(String.format("The user having ID or email %s was not found", userId));
else
userToBeUpdated = retrievedUser.get();
return userToBeUpdated
.map((userToBeUpdated) -> {
log.info(String.format("****** USER E-MAIL *******", userToBeUpdated.getEmail()));
userToBeUpdated.set_active(isActive);
this.userRepository.save(userToBeUpdated);
return userToBeUpdated;
})
.orElseThrow(() -> new NotFoundException(String.format("The user having ID or email %s was not found", userId)));
}
我的想法是使用 map() 方法将一个对象(我的 User 对象转换为 UserDTO 对象并保存它在数据库上应用一个函数)。
首先 Eclipse 它在 map() 方法调用上给我以下错误:
方法 map(( userToBeUpdated) -> {}) 未定义 输入用户
由于我对流的经验很少,我不知道它是否是 map() 使用的正确用例。这是因为(也在第一个方法实现中,效果很好)我没有实现真正的对象转换,但我正在更改对象字段的值,然后将其保存到数据库中。在第二种情况下(我的服务方法的第二个版本),我必须从 User 转换为 UsertDTO。
我错过了什么?在这个用例中使用 map() 是否合法,还是强制使用 map() 的预期方式?有什么好的方法可以解决这个问题?
【问题讨论】:
标签: java spring-boot java-8 java-stream java-14