【发布时间】:2020-05-26 14:31:57
【问题描述】:
我在下面的代码中使用 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 的完整跟踪
感谢任何帮助。提前致谢。
编辑- 有什么方法可以检查/打印发出 get 请求的 restTemplate 中的 url?
【问题讨论】:
标签: java spring spring-boot kotlin resttemplate