我得到一個奇怪的問題,我困在它至少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")
}
確定什麼是錯在上面,我不知道是什麼任何人都可以請幫助?
你能後的實際代碼? –
您必須在可見性範圍內具有其他名稱爲'methodB'的其他名稱。閉包,變量,不管。實際代碼爲+1。 –
嗨勝利者,蒂姆:對於遲到的迴應抱歉,但沒有這樣的..只是從一個UtilController我打電話給服務方法A,並從該方法A在服務我打電話給另一個methodB方法。現在一切都很好,直到methodB返回的東西,它在methodA中不可用,但是如果我在UtilController中寫入methodB,我得到返回值..我認爲它與我的服務有關,因爲它實現了InitializingBean,GroovyInterceptable –