1

在 play 2.0 应用程序中调用 web 服务时遇到问题。这是我的代码的外观

Future<Object> promise = WS.url("http://myurl").get().map(testFunc1, null);
 Function1 testFunc1 = new Function1(){
    public void $init$() {}
    public Object apply(Object v1) {
        System.out.println("apply");
        return "";
    }
    public Function1 andThen(Function1 g) { return null; }
    public Function1 compose(Function1 g) {return null;}
};

但是我的 ide 给我一个编译时异常说

error: <anonymous MyClass$1> is not abstract and does not override abstract method andThen$mcVJ$sp(Function1) in Function1
     Function1 testFunc1 = new Function1(){

我有这些包导入

import play.api.libs.ws.WS;
import scala.Function1;
import scala.concurrent.Future;

显然,我似乎在这里遗漏了一些东西。谁能告诉我它是什么。或者我什至需要用 Function1 映射 promise 对象?

谢谢卡提克

4

1 回答 1

2

您的代码看起来像 Java,但您使用的是 Scala 库。该软件包play.api适用于 Scala API。

采用

import play.libs.WS;
import play.libs.F.Function

代替

import play.api.libs.ws.WS;
import scala.Function1;

例子

//checkout https://github.com/schleichardt/stackoverflow-answers/tree/so18491305
package controllers;

import play.libs.F.Function;
import play.libs.F.Promise;
import play.mvc.*;
import play.libs.WS;

public class Application extends Controller {
    /**
     * This action serves as proxy for the Google start page
     */
    public static Result index() {
        //Phase 1 get promise of the webservice request
        final Promise<WS.Response> responsePromise = WS.url("http://google.de").get();
        //phase 2 extract the usable data from the response
        final Promise<String> bodyPromise = responsePromise.map(new Function<WS.Response, String>() {
            @Override
            public String apply(WS.Response response) throws Throwable {
                final int statusCode = response.getStatus();
                return response.getBody();//assumed you checked the response code for 200
            }
        });
        //phase 3 transform the promise into a result/HTTP answer
        return async(
                bodyPromise.map(
                        new Function<String,Result>() {
                            public Result apply(String s) {
                                return ok(s).as("text/html");
                            }
                        }
                )
        );
    }
}
于 2013-08-28T20:48:11.250 回答