2015-10-28 75 views
0

我嘗試從另一個Grails應用程序(MyApplication2)獲取Grails應用程序(MyApplication2)的數據。 Boths應用程序在本地運行。 我得到一個使用下面的代碼引發的異常。Groovy/Grails - 使用HTTPBuilder獲取HttpResponseException

Groovy的

try { 
    def http = new HTTPBuilder('http://localhost:8080/MyApplication2') 

    http.get(path : '/ControllerName/Action', contentType : ContentType.TEXT) { resp, reader -> 

      println "response status: ${resp.statusLine}" 
      resp.headers.each { h -> 
       println " ${h.name} : ${h.value}" 
      } 

     } 
} catch (groovyx.net.http.HttpResponseException ex) { 
    ex.printStackTrace() 
    println ex.toString() 

} catch (java.net.ConnectException ex) { 
    ex.printStackTrace() 
    println ex.toString()  
}  

拋出異常

Error 2015-10-28 10:13:43,448 [http-bio-8090-exec-1] ERROR errors.GrailsExceptionResolver - HttpResponseException occurred when processing request: [GET] /MyApplication1/ControllerName/Action Not Found. 
+0

try catch? https://github.com/vahidhedayati/grails-jenkins-plugin/blob/master/src/main/groovy/grails/plugin/jenkins/HBuilder.groovy#L25-L30 – Vahid

+0

我加了try catch,「groovyx.net。 http.HttpResponseException:未找到「被捕獲 – Jils

+0

嗯,也許擺脫在控制器/行動塊的結尾額外/? – billjamesdev

回答

0

我終於發現了問題:我改變了URI格式。 我在URI的末尾添加了一個/,並且我在path的開頭刪除了/

try { 

     def http = new HTTPBuilder('http://localhost:8080/MyApplication2/') 

     http.get(path : 'ControllerName/Action', contentType : ContentType.TEXT) { resp, reader -> 

     println "response status: ${resp.statusLine}" 
     resp.headers.each { h -> 
      println " ${h.name} : ${h.value}" 
     } 

    } 
} catch (groovyx.net.http.HttpResponseException ex) { 
    ex.printStackTrace() 
    println ex.toString() 

} catch (java.net.ConnectException ex) { 
    ex.printStackTrace() 
    println ex.toString()  
}