2011-07-14 76 views
0

我得到一個奇怪的問題,我困在它至少4個小時了。其實我已經寫了我的代碼在一個控制器進行測試,但是當我將代碼移到服務時,我得到一個奇怪的行爲,即服務中的方法沒有返回,或者可能是在服務中調用它們的方法只接收不到。退貨不能在服務

class FacebookService implements InitializingBean, GroovyInterceptable { 
def getUserLikes(def at){ 
List<String> listOfUrls = [] 
    String basicFbUrl = "https://graph.facebook.com/" 
    String likeUrl = basicFbUrl + "me/likes?access_token=${at}" 
    URL url = new URL(likeUrl) 
    String jsonResponse = getResponseFromUrl(url) 
    println "JSON RESPONSE IS ${jsonResponse}" // this is showing null 
} 

String getResponseFromUrl() { 
    String something 

    String resp = null; 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    try { 
     int respCode = conn.responseCode 
     if (respCode == 400) { 
      log.error("COULD NOT MAKE CONNECTION") 
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getErrorStream())); 
      def jsonResp = JSON.parse(br.text) 
     } else { 
      resp = conn.getInputStream().getText() 
     } 
    } finally { 
     conn.disconnect() 
    } 

    println("RETURNIG RESPONSE ${resp}") // This returns me a map as expected 

    return resp; 
} 

不知道resp在哪裏?請提供任何建議? OK我知道的罪魁禍首,我張貼invokeMethod中的代碼

def invokeMethod(String name, args){ 
    System.out.println("IN INVOKE METHOD NAME ${name}") 
    if(facebookPalsCache==null) 
     facebookPalsCache = new FacebookPalsCache(1000) 
    System.out.println("time before ${name} called: ${new Date()}") 

    //Get the method that was originally called. 
    def calledMethod = metaClass.getMetaMethod(name, args) 
    System.out.println("CALLED METHOD IS ${calledMethod}") 

    //The "?" operator first checks to see that the "calledMethod" is not 
    //null (i.e. it exists). 
    if(name.equals("getFriends")){ 
     println "getFriends..." 
     def session = RequestContextHolder.currentRequestAttributes().getSession() 
     def friends = facebookPalsCache.get(session.facebook.uid) 
     if(!friends){ 
      def getFriends = facebookGraphService.invokeMethod (name, args) 
      println "Saving FBFRIENDS in CACHE" 
      facebookPalsCache.put(session.facebook.uid, getFriends) 
      return getFriends 
     } 
     else return friends 
    } 

    else { 
     if(calledMethod){ 
      System.out.println("IN IF AND INVOKING METHOD ${calledMethod}") 
      calledMethod.invoke(this, args) 
     } 
     else { 
      return facebookGraphService.invokeMethod(name, args) 
     } 
    } 
    System.out.println "RETURNING FROM INVOKE METHOD FOR NAME ${name}" 
    System.out.println("time after ${name} called: ${new Date()}\n") 
} 

確定什麼是錯在上面,我不知道是什麼任何人都可以請幫助?

+1

你能後的實際代碼? –

+1

您必須在可見性範圍內具有其他名稱爲'methodB'的其他名稱。閉包,變量,不管。實際代碼爲+1。 –

+0

嗨勝利者,蒂姆:對於遲到的迴應抱歉,但沒有這樣的..只是從一個UtilController我打電話給服務方法A,並從該方法A在服務我打電話給另一個methodB方法。現在一切都很好,直到methodB返回的東西,它在methodA中不可用,但是如果我在UtilController中寫入methodB,我得到返回值..我認爲它與我的服務有關,因爲它實現了InitializingBean,GroovyInterceptable –

回答

1

invokeMethod和服務的代碼似乎並不相同,除非有單獨的FacebookGraphService。假設是這種情況,那麼resp正在if (calledMethod) {區塊內的invokeMethod部分中被捕獲,但由於它不是該方法的最後一行,因此它不會從調用invokeMethod返回,因此正在被吞噬。

嘗試增加一回calledMethod.invoke(this, args)

if(calledMethod){ 
     System.out.println("IN IF AND INVOKING METHOD ${calledMethod}") 
     return calledMethod.invoke(this, args) 
    } 
+0

非常棒非常感謝你plecong。 –