Reputation: 161
I am retrieving a publisher Mono<ProductOrder>
from mongo db
I have another publisher Mono<CommandResponse>
I want to set ProductOrder
object into CommandResponse
as it is a part of that class. like commandResponse.setProductOrder(po instance)
CommandResponse
will also have different Mono
or String or Int
apart from ProductOrder
instance
Finally want to return Mono<ServerResponse>
which would contain body of CommandResponse
i am not being able to set the Mono<ProductOrder>
object into Mono<CommandResponse>
Pleas help. Thanks.
CODE SNIPPET
@Component
public class Handler {
@Autowired
private MongoService service;
public Mono<ServerResponse> get(ServerRequest request) {
Mono<ProductOrder> p = service.queryById();
> Here, in the return statement i want to return Mono<CommandResponse>
> instead of Mono<ProductOrder> in the response body
> remember: CommandResponse has a reference to ProductOrder
return
ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(p, ProductOrder.class).map(null);
);
}
}
Upvotes: 0
Views: 367
Reputation: 1082
Mono<CommandResponse> commandMono = ... // I don't how it is set
Mono<ProductOrder> productMono = service.queryById();
Mono.zip(commandMono, productMono)
.map(tuple -> {
var command = tuple.getT1();
var product = tuple.getT2();
command.setProductOrder(product) // => .setProductOrder(po instance)
return command;
})
.flatMap(command -> ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(command)
)
I haven't run this code, so I'm not sure if it compiles, but with it you should be able to figure out how your code could work
Upvotes: 2