我尝试从另一个Grails应用程序(MyApplication2)获取Grails应用程序(MyApplication2)的数据。两者都在本地运行。
我使用以下代码引发异常。
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.
最佳答案
我终于找到了问题:我更改了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()
}
关于rest - Groovy/Grails-通过使用HTTPBuilder获得HttpResponseException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33387753/