使用 RestTemplate 的 Http Get 请求 - org.springframework.web.client.ResourceAccessException
Http Get request with RestTemplate - org.springframework.web.client.ResourceAccessException
我在下面的代码中使用 RestTemplate 发出 GET 请求。如果我直接从 chrome/postman 调用它,请求工作正常。但是,它不适用于代码。它似乎忽略了 rootUri
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
@Component
class Provider(
private val builder: RestTemplateBuilder
) {
var client: RestTemplate = builder.rootUri("http://foo.test.com/API/rest").build()
fun sendMessage(request: Request) {
println("here is the request")
try {
val resp = client.getForEntity(
"?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
)
} catch (e: Exception) {
println("this is the error")
e.printStackTrace()
}
}
}
这是我遇到的异常。
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "": null; nested exception is org.apache.http.client.ClientProtocolException
....
....
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187)
Caused by: org.apache.http.client.ClientProtocolException
....
....
Caused by: org.apache.http.ProtocolException: Target host is not specified
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:71)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:125)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
Caused by: org.apache.http.ProtocolException: Target host is not specified
完整跟踪 http://collabedit.com/t6h2f
感谢任何帮助。提前致谢。
编辑- 有什么方法可以 check/print 来自 restTemplate 的 url 吗?
您错过了一件事,如果您查看 RestTemplateBuilder.rootUri(..) 文档,他们会将 rootUri 设置为以 /
开头的任何请求。但是如果你没有添加它,它会忽略 rootUri 值。
因此,如果您只更改对此的调用,它将起作用:
val resp = client.getForEntity(
"/?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
)
更新:
var client: RestTemplate = builder.rootUri("http://foo.test.com/").build()
fun sendMessage(request: Request) {
println("here is the request")
try {
val resp = client.getForEntity(
"/API/rest?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
)
} catch (e: Exception) {
println("this is the error")
e.printStackTrace()
}
}
我在下面的代码中使用 RestTemplate 发出 GET 请求。如果我直接从 chrome/postman 调用它,请求工作正常。但是,它不适用于代码。它似乎忽略了 rootUri
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
@Component
class Provider(
private val builder: RestTemplateBuilder
) {
var client: RestTemplate = builder.rootUri("http://foo.test.com/API/rest").build()
fun sendMessage(request: Request) {
println("here is the request")
try {
val resp = client.getForEntity(
"?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
)
} catch (e: Exception) {
println("this is the error")
e.printStackTrace()
}
}
}
这是我遇到的异常。
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "": null; nested exception is org.apache.http.client.ClientProtocolException
....
....
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187)
Caused by: org.apache.http.client.ClientProtocolException
....
....
Caused by: org.apache.http.ProtocolException: Target host is not specified
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:71)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:125)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
Caused by: org.apache.http.ProtocolException: Target host is not specified
完整跟踪 http://collabedit.com/t6h2f
感谢任何帮助。提前致谢。
编辑- 有什么方法可以 check/print 来自 restTemplate 的 url 吗?
您错过了一件事,如果您查看 RestTemplateBuilder.rootUri(..) 文档,他们会将 rootUri 设置为以 /
开头的任何请求。但是如果你没有添加它,它会忽略 rootUri 值。
因此,如果您只更改对此的调用,它将起作用:
val resp = client.getForEntity(
"/?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
)
更新:
var client: RestTemplate = builder.rootUri("http://foo.test.com/").build()
fun sendMessage(request: Request) {
println("here is the request")
try {
val resp = client.getForEntity(
"/API/rest?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
)
} catch (e: Exception) {
println("this is the error")
e.printStackTrace()
}
}