Reputation: 31
I'm using Reactive programming in Java Spring Boot. I am getting data from an end point using WebClient and .bodyToFlux.
This data is of the form:
{"name":"John Doe","details":{"phone":"1234567890","location":"Antarctica"},"phone":null,"location":null}
^The Flux is Flux<Information>
I have a class called Information
which has the following fields and constructors/get/sets for them:
String name;
Details details;
String phone;
String location;
I have another class called Details
which has the following fields and constructors/get/sets for them:
String phone;
String location;
When I get the data from the end point using WebClient and .bodyToFlux, it doesn't get data for the phone and location fields for the Information object itself (They are fetched as null), but it gets it for the phone and location fields in Details object in the Information object.
So, in order to get the phone and location data from the Details object to store it in the phone and location fields in the Information object, I do:
.doOnNext(i -> {
i.setPhone(i.getDetails().getPhone());
i.setLocation(i.getDetails().getLocation()
});
So, then I get: {"name":"John Doe","details":{"phone":"1234567890","location":"Antarctica"},"phone":"1234567890","location":"Antarctica"}
Now comes the tricky part. I want to 'get rid' of the Details object in the Information object since I already got the phone and location data from it.
I have a duplicate class called InformationWithoutDetailsObject
and it the following fields and constructors/get/sets for them:
String name;
String phone;
String location;
I want to convert Flux<Information>
into Flux<InformationWithoutDetailsObject>
. How would I achieve this? I can't use blocking since it's supposed to be Reactive.
This is my code:
public Flux<InformationWithoutDetailsObject> getInformationStream () throws IOException {
information = webClient
.get()
.uri(url)
.retrieve()
.bodyToFlux (Information.class)
.doOnNext(i ->storeCompanyNameAndResourceType(i));
return information;
}
public void storeCompanyNameAndResourceType(Information information) {
information.setPhone(information.getDetails.getPhone);
information.setLocation(information.getDetails.getLocation);
}
Upvotes: 0
Views: 3250
Reputation: 31
In case anyone else is wondering, I fixed it by making the information variable of type Flux<InformationWithoutDetailsObject>
(It was of the type Flux<Information>
first), and then using .map to 'convert' the Information
objects to InformationWithoutDetailsObject
objects.
public Flux<InformationWithoutDetailsObject> getInformationStream () throws IOException {
informationWithoutDetailsObject = webClient
.get()
.uri(url)
.retrieve()
.bodyToFlux (Information.class)
.doOnNext(i ->storeCompanyNameAndResourceType(i));
.map(k -> new InformationWithoutDetailsObject(
k.getPhone(),
k.getLocation());
return informationWithoutDetailsObject;
}
public void storeCompanyNameAndResourceType(Information information) {
information.setPhone(information.getDetails.getPhone);
information.setLocation(information.getDetails.getLocation);
}
.map(....) also worked when I didn't change the Flux type, but yeah, do try this in case the first approach doesn't work for some reason.
Upvotes: 2