matrixguy
matrixguy

Reputation: 306

Http Get request with RestTemplate - org.springframework.web.client.ResourceAccessException

I am making a GET Request using RestTemplate in the below code. The request works fine if i directly call it from chrome/postman . However, its not working from the code. It seems to be ignoring the 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()
        }
    }

}

This is the exception i am getting.

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

Full trace at http://collabedit.com/t6h2f

Any help is appreciated. Thanks in advance.

Edit- Any way to check/print the url from restTemplate on which get request is made?

Upvotes: 0

Views: 2600

Answers (1)

Ibrahim AlTamimi
Ibrahim AlTamimi

Reputation: 632

You miss one thing, If you look at RestTemplateBuilder.rootUri(..) documentation, they set rootUri into any request start with /. but if you didn't add it, it will ignore rootUri value.

So if you only change your call to this it will work:

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

Reference

Updates:

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

Upvotes: 1

Related Questions