2013-08-28 8 views
1

J'ai des problèmes pour appeler un service Web dans une application Play 2.0. Voici comment mon code ressembleAppel de service Web depuis Play 2.0

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

Mais mon ide me lancer une exception de temps de compilation en disant

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

Je ces paquets importés

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

Il est clair que je semble manquer quelque chose ici. Quelqu'un peut-il me dire ce que c'est. Ou ai-je même besoin de mapper l'objet de promesse avec Function1?

Merci Karthik

Répondre

2

Votre code ressemble à Java, mais vous utilisez les bibliothèques Scala. Le package play.api est destiné à l'API Scala.

utilisation

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

au lieu de

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

Exemple

//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"); 
          } 
         } 
       ) 
     ); 
    } 
}