I new to Spring boot microservices and exploring webflux framework. I'm trying to merge response from two microservices to one wrapper class to have the response in proper JSON. Below is the scenario in detail.
Micro-Service 1 : http://localhost:8080/products
in controller of this micro-service its returning Flux and I'm get
[
{
"id": "5b2fd1e5f57d731904c54ad7",
"name": "Product3",
"price": "30"
},
{
"id": "5b2fd1e4j9fdj3kds9djkj43",
"name": "Product2",
"price": "20"
}
]
Micro-Service 2 : http://localhost:8181/person
In controller of second service its returning Mono and for this also I'm getting correct response as below,
{
"id": ehj8u3jmodmdj,
"name": "PersonXXX",
"email": "PersonXXX@somecorp.com"
}
Now I want to create another microservice http://localhost:8282/personproduct which should combine the result of above two microservices in a wrapper class as below,
{
{
"id": ehj8u3jmodmdj,
"name": "PersonXXX",
"email": "PersonXXX@somecorp.com"
},
[
{
"id": "5b2fd1e5f57d731904c54ad7",
"name": "Product3",
"price": "30"
},
{
"id": "5b2fd1e4j9fdj3kds9djkj43",
"name": "Product2",
"price": "20"
}
]
}
Right now I have a Parent class Entity for both Product and Person classes and I'm calling both above mentioned micro-services via WebClient and concatinating the response using Flux.concat(personResp, productResp); where personResp is of type Mono and productResp is of type Flux but I'm getting response of this (3rd) microservice only in Text and not in JSON as below,
data:{"id":ehj8u3jmodmdj,"name":"PersonXXX","email":"PersonXXX@somecorp.com"}
data:{"id":"5b2fd1e5f57d731904c54ad7","name":"Product3","price":"30"}
data:{"id":"5b2fd1e4j9fdj3kds9djkj43","name":"Product2","price":"20"}
This might be due to each element is sent as a different stream.
So just want to know if is there any way to combine two responses in one wrapper class without using block() method on any of the calls done to these two services.
UPDATE
Currently Im calling product microservice as,
clientProd.get().uri(productUrl)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve().bodyToFlux(Product.class).onErrorReturn(new Product());
And similarly Person service as,
clientPerson.get().uri(personUri)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve().bodyToMono(Person.class).onErrorReturn(new Person());
And concatenating is using Flux.concat(),
Thanks in advance.
See Question&Answers more detail:
os