我需要將屬性Friends(它是一個 ArrayList)從 a復制Mono<PersonEntity>到 a Mono<UserEntity>(它Friends在資料庫中沒有屬性),但我沒有找到正確的方法來做到這一點,所以當我映射Mono<UserEntity>到 Dto 時,欄位Friends結果是一個空陣列[]。
public Mono<Dto> findEntityByIdAndLabel(Long id, String label) {
return getPersonByIdAndLabel(id, label).flatMap(person -> {
return UserRepository.findByID(id);
})
.switchIfEmpty(Mono.error(new EntityNotFoundException(entity.toString(), id.toString(), label)))
.map(this::mapper);
}
我想我應該在之后添加一些東西,findById(id)但直到現在我嘗試過的一切都不起作用。謝謝你的時間
uj5u.com熱心網友回復:
由于您沒有顯示PersonEntity,UserEntity我們只能猜測它們的屬性以及 getter 和 setter 方法。盡管如此,以下幾行應該可行:
public Mono<Dto> findEntityByIdAndLabel(Long id, String label) {
return getPersonByIdAndLabel(id, label)
.zipWith(UserRepository.findByID(id))
.map(tuple -> {
List friends = tuple.getT1().getFriends();
UserEntity user = tuple.getT2():
user.setFriends(friends);
return user;
})
.switchIfEmpty(Mono.error(new EntityNotFoundException(entity.toString(), id.toString(), label)))
.map(this::mapper);
}
重要的是zipWith,它將 aMono和 another的結果組合Mono成 a Tuple2,然后您可以輕松映射。您可以在參考檔案中閱讀有關此內容的更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365171.html
標籤:爪哇 春天 映射 弹簧-webflux
