首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用org.springframework.web.client.ResourceAccessException - RestTemplate获取请求

使用org.springframework.web.client.ResourceAccessException - RestTemplate获取请求
EN

Stack Overflow用户
提问于 2020-05-26 14:31:57
回答 1查看 1.4K关注 0票数 0

在下面的代码中,我使用RestTemplate发出了一个GET请求。如果我直接从chrome/postman调用它,请求就能正常工作。但是,它不能从代码中运行。它似乎忽略了rootUri

代码语言:javascript
运行
复制
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()
        }
    }

}

这是我得到的例外。

代码语言:javascript
运行
复制
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上进行完全跟踪

任何帮助都是非常感谢的。提前谢谢。

编辑-有任何方式检查/打印来自restTemplate的url,而get请求是在其上提出的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-26 15:38:08

如果你看RestTemplateBuilder.rootUri(..),你会错过一件事。文档中,他们将rootUri设置为任何以/开头的请求。但是如果您没有添加它,它将忽略rootUri值。

因此,如果您只更改了这个调用,它就会起作用:

代码语言:javascript
运行
复制
val resp = client.getForEntity(
                "/?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
            ) 

参考文献

更新:

代码语言:javascript
运行
复制
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()
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62024543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档