返回不在服务中

时间:2011-07-14 10:35:13

标签: grails groovy

我遇到了一个奇怪的问题,我现在至少坚持了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在哪里?有什么建议吗? 好的,我知道罪魁祸首,我发布了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 个答案:

答案 0 :(得分:1)

除非有单独的invokeMethod,否则FacebookGraphService和服务的代码似乎不一样。假设是这种情况,那么respinvokeMethod块中的if (calledMethod) {部分捕获,但由于它不是方法的最后一行,因此它不会从拨打invokeMethod,因此被吞并了。

尝试添加返回calledMethod.invoke(this, args)

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