【问题标题】:Http Get request with RestTemplate - org.springframework.web.client.ResourceAccessException使用 RestTemplate 获取 Http 请求 - org.springframework.web.client.ResourceAccessException
【发布时间】: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


    【解决方案1】:

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

    因此,如果您仅将调用更改为此它将起作用:

    val resp = client.getForEntity(
                    "/?userid=123456&password=1234356&method=TEST_MESSAGE", Response::class.java
                ) 
    

    Reference

    更新:

    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()
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多