以反应方式创建DTO的正确方法是什么?

问题描述 投票:0回答:1

我使用Spring Boot并且我有一些Reactive API(Controller调用一个基于Mongo Reactive Repositories调用存储库的服务)

由于我不希望用户直接看到我的模型对象,我想有DTO。

一个DTO可以基于几个模型的信息。

例如:给出两个模型项

ItemA:
 infoA:String

ItemB:
 infoB:String

我想创建一个DTO项目

ObjectDTO:
 infoA: String
 infoB: List<ItemB>

在我的对象模型和DTO之间反应映射数据的正确方法是什么?

spring-boot dto reactive reactor reactor-netty
1个回答
0
投票

只需使用Mono.zip(https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#zip-reactor.core.publisher.Mono-reactor.core.publisher.Mono-java.util.function.BiFunction-)将2个结果合并为一个。

return Mono.zip(repo1.getItemA(), repo2.getItemB(),
   (itemA, itemB) -> new ObjectDTO(itemA.getInfoA(), itemB.getInfoB())
);

返回itemS和itemS的方法应该返回一个qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.