Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - How to merge two spring boot micro-services response in wrapper class

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The correct way is to map those responses to classes:

public class Product{
    private Long id;
    private String name;
    private Double price;

    //constructors, getters, setters
}

public class Person{
    private Long id;
    private String name;
    private String mail;

    //constructors, getters, setters
}

public class Entity{
    private Person person;
    private List <Product> products;

    //constructors, getters, setters
}

In this way you have three different POJOs that you can use according to the needs (the type of API you are calling).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...