我正在開發一個 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物件。首先,我不確定回傳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) -> {}) 對于 User 型別未定義
由于我對流的經驗很少,我不知道它是否是map()使用的正確用例。這是因為(也在第一個方法實作中,效果很好)我沒有實作真正的物件轉換,但我正在更改物件欄位的值,然后將其保存到資料庫中。在第二種情況下(我的服務方法的第二個版本),我必須從User轉換為UsertDTO。
我錯過了什么?在這個用例中使用map()是否合法,還是強制使用 map() 的預期方式?什么可能是解決這個問題的好方法?
uj5u.com熱心網友回復:
在第一個示例中,該if宣告完全沒有意義。您可以完全洗掉它,因為如果回傳的實際值不是 ,則將執行mapon 。此外,還有一個代碼重復。OptionalnullNotFoundException
@Override
public UserDTO activateDeactivateUser(int userId, boolean isActive) throws NotFoundException {
return this.userRepository.findById(userId)
.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. 當我們將其作為映射操作的高階函式傳遞時,建議使用純函式。使用這樣的東西可能會更好:
@Override
public User activateDeactivateUser(int userId, boolean isActive) throws NotFoundException {
User retrievedUser = this.userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(String.format("The user having ID or email %s was not found", userId)));
retrievedUser.set_active(isActive);
this.userRepository.save(retrievedUser);
return retrievedUser;
}
現在,您的第二個示例更有問題。這個片段是無效的,在邏輯上沒有意義:
userToBeUpdated
.map((userToBeUpdated) -> {...})
User類是一個休眠物體,它很可能沒有map實作它的方法,為什么會呢?在 Java 中,通常流和可選項為map. 它用于對流/可選封裝的每個物件應用一些轉換。您可能想查看流和選項的檔案以了解更多關于它們的資訊。
我相信你想在這里做的是:
@Override
public UserDTO activateDeactivateUser(int userId, boolean isActive) throws NotFoundException {
User retrievedUser = this.userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(String.format("The user having ID or email %s was not found", userId)));
retrievedUser.set_active(isActive);
this.userRepository.save(retrievedUser);
return conversionService.convert(retrievedUser);
}
我希望它ConversionService有類似convert方法的東西,它接受一個物體物件并回傳一個 DTO。
與該問題無關的其他一些注釋:
- 請遵守 Java 編碼標準。在 Java 中,我們不使用蛇形大小寫進行方法呼叫。這
userToBeUpdated.set_active(isActive);應該是這樣的userToBeUpdated.setActive(isActive);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/414762.html
標籤:
