我需要將屬性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)
但直到現在我嘗試過的一切都不起作用。謝謝你的時間
由於您沒有顯示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
,然後您可以輕鬆映射。您可以在參考文檔中閱讀有關此內容的更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句