We have the following code to retrieve data from a third party.
// Full URL with symbol and query parameter structure
String url = "https://query.theapp.theservice.com/v1/abc/foo/" + foo;
// Prepare query parameters including the crumb and module type
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
queryParams.add("boo", boo);
// Execute the GET request using RestClient and pass full URL with query parameters
ResponseEntity<String> response = restClient.get()
.uri(uriBuilder -> uriBuilder
.replacePath(url) // Use replacePath() with the full URL
.queryParams(queryParams) // Add query parameters
.build())
.header(HttpHeaders.USER_AGENT, "Mozilla/5.0")
.retrieve()
.toEntity(String.class);
With the above code, we get a runtime error:
I/O error on GET request for "https://query.theapp.theservice.com/v1/abc/foo/theFoo": Target host is not specified
We can't find queryParams, but @RequestParam in the document.
We don't know the correction of the following explanation of the error.
The error message "Target host is not specified" suggests that the URL is still not being constructed correctly. This often occurs when the URI handling within RestClient doesn't interpret the URL as fully qualified.
Comment From: bclozel
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
In this case I don't think you are using the following method as intended: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriBuilder.html#replacePath(java.lang.String)