Grails 返回未在服务中工作
我遇到了一个棘手的问题,我现在至少被困了4个小时。实际上,我已经在一个控制器中编写了用于测试的代码,但是当我将代码移动到服务中时,我得到了一个奇怪的行为,即服务中的方法没有返回,或者可能是仅在服务中调用它们的方法没有接收Grails 返回未在服务中工作,grails,groovy,Grails,Groovy,我遇到了一个棘手的问题,我现在至少被困了4个小时。实际上,我已经在一个控制器中编写了用于测试的代码,但是当我将代码移动到服务中时,我得到了一个奇怪的行为,即服务中的方法没有返回,或者可能是仅在服务中调用它们的方法没有接收 class FacebookService implements InitializingBean, GroovyInterceptable { def getUserLikes(def at){ List<String> listOfUrls = [] S
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;
}
好的,上面有点问题,我不知道有什么人可以帮忙吗?调用方法的代码和服务似乎不一样,除非有一个单独的
facebook graphservice
。假设是这种情况,那么resp
被捕获在invokeMethod
部分,该部分位于if(calledMethod){
块中,但由于它不是方法的最后一行,因此不会从调用invokeMethod
返回,因此被占用
尝试将返回添加到calledMethod.invoke(this,args)
:
你能发布实际的代码吗?你必须在可见性范围内有其他名为
methodB
的东西。闭包、变量等等。实际代码为+1。嗨,维克托,蒂姆:很抱歉反应太晚,但没有这样的东西。只是从一个UtilController我调用了服务methodA,从我调用的服务中的那个methodA另一个方法是methodB。现在一切都很好,直到methodB返回一些东西,它在methodA中不可用,但是如果我在UtilController中编写methodB,我会得到返回值。我认为这与我的服务有关,因为它实现了初始化Bean,GroovyInterceptable可能你的拦截器弄乱了返回值我现在已经分享了我的代码,也许现在有人可以解释一下???太棒了,非常感谢你。
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")
}
if(calledMethod){
System.out.println("IN IF AND INVOKING METHOD ${calledMethod}")
return calledMethod.invoke(this, args)
}