【问题标题】:Multiple WS call in one action, how to handle Promise objects in java play framework?多个WS一次调用,java play框架中如何处理Promise对象?
【发布时间】:2015-07-23 13:40:10
【问题描述】:

我在 PlayFramework2/Java 中开发了一个小服务器,它必须从多个 WS (REST/JSON) 中检索数据,从这些 WS 中操作数据,然后组合并返回结果。

我知道如何调用一个 WS,操作数据并返回响应。但我不知道如何连续调用多个网络服务,处理每次调用之间的数据并生成汇总答案。

请帮忙..提前谢谢

【问题讨论】:

    标签: java web-services playframework-2.2


    【解决方案1】:

    您可以使用 F.Promise.sequence 组合两个或多个响应。试试下面的代码

     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;
       }
    

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 2020-04-22
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多