Reputation: 63
I develop a little server in PlayFramework2/Java which has to retrieve data from multiple WS (REST/JSON), manipulate the data from theses WS, then compose and return a result.
I know how to call one WS, manipulate the data and return response. But I don't know how how to call successively several web-services, handle the data between every call and generate an aggregated answer.
Please help.. Thanks in advance
Upvotes: 2
Views: 753
Reputation: 36
You can use F.Promise.sequence for combining two or more responses. Try the below code
public static Promise<Result> selectFeed() {
F.Promise<WS.Response> response1 = WS.url(<firstUrl>).get();
F.Promise<WS.Response> response2 = WS.url(<SecondUrl>).get();
F.Promise<List<WS.Response>> responses = F.Promise.sequence(response1, response2);
F.Promise<Result> resultPromise= responses.map(new F.Function<List<WS.Response>,Result>() {
@Override
public Result apply(List<WS.Response> o) throws Throwable {
//some code;
String s= o.get(0).asJson().toString();
String s2 = o.get(1).asJson().toString();
return ok(s+s2);
}
});
return resultPromise;
}
Upvotes: 2