I am trying to implement the BodyExtractor interface get the body from Mono< ClientResponse> as an Object instead of it in Mono.
I could not find any example of BodyExtractor implementation. I am wondering is this a good idea to implement it or is there any other way to get the body as an object.
Below is the code line that I currently have
public Mono<ResponseEntity<Mono<JsonNode>>> processUnmappedApiRequest(ServerHttpRequest request, JsonNode body) {
RequestData reqData = this.prepareReqMetadata(request, body);
Mono<ClientResponse> response = commonConnector.getApiResponse(reqData);
return response.map(respData -> {
int latestVersion = respData.headers().header("version").size() == 0 ? getLatestVersion(request) :
Integer.parseInt(respData.headers().header("version").get(0));
List converterList;
if((converterList = converterSequenceProvider.getConverterList(reqData.getRequestPath(), latestVersion, reqData.getVersion())) != null){
return ResponseEntity.ok().body(respData.bodyToMono(JsonNode.class).map(respBody -> convertToDesiredVersion(converterList, respBody)));
}
return ResponseEntity.ok().body(respData.bodyToMono(JsonNode.class));
});
}
In this method my return type is Mono< ResponseEntity< Mono< JsonNode>>> and I am trying to convert it to Mono< ResponseEntity< JsonNode>> because my team is not agreeing with Mono inside a Mono.
So the main point here is I don't want to use bodyToMono method and I am not sure how to use body method.
Please help me out here.
If you are trying to return just a Mono object, you can use the flatMap method instead of map, so you can avoid something like Mono<Mono<X>> and get just Mono<X>.
map
Transform the item emitted by this Mono by applying a synchronous function to it.
flatMap
Transform the item emitted by this Mono asynchronously, returning the value emitted by another Mono (possibly changing the value type).
Also, there is a method on ServerResponse.BodyBuilder syncBody which can take normal body and return it in Mono. Map function's argument is already a non wrapped object, so you can do something like:
JsonNode jsonNode=transform(clientResponse);
return ResponseEntity.ok().syncbody(jsonNode);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With