Playframework 从Play 2.0中调用webservice
在play 2.0应用程序中调用Web服务时遇到问题。下面是我的代码的外观Playframework 从Play 2.0中调用webservice,playframework,playframework-2.0,promise,Playframework,Playframework 2.0,Promise,在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.printl
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;}
};
很明显,我似乎遗漏了一些东西。谁能告诉我是什么吗。或者我甚至需要用Function1映射promise对象吗
谢谢
Karthik您的代码看起来像Java,但您使用的是Scala库。 包
play.api
是针对Scala api的
使用
而不是
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");
}
}
)
);
}
}
//签出https://github.com/schleichardt/stackoverflow-answers/tree/so18491305
包装控制器;
导入play.libs.F.函数;
导入play.libs.F.Promise;
导入play.mvc.*;
导入play.libs.WS;
公共类应用程序扩展控制器{
/**
*此操作充当Google起始页的代理
*/
公共静态结果索引(){
//阶段1获得webservice请求的承诺
最终承诺响应Promise=WS.url(“http://google.de).get();
//阶段2从响应中提取可用数据
final Promise bodyPromise=responsePromise.map(新函数(){
@凌驾
公共字符串apply(WS.Response-Response)抛出可丢弃的{
final int statusCode=response.getStatus();
return response.getBody();//假设您检查了200的响应代码
}
});
//阶段3:将承诺转换为结果/HTTP答案
异步返回(
bodyPromise.map(
新函数(){
公共结果应用(字符串s){
将ok(s.)返回为(“文本/html”);
}
}
)
);
}
}
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");
}
}
)
);
}
}