My request url is http://my-host.1domain:1234/path
if i use RestClient
ResponseEntity<BackResponse> response = restClient.post().uri(url)
.header("hs", config.getSecret())
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.retrieve()
.toEntity(BackResponse.class);
will cause
` java.lang.IllegalArgumentException: unsupported URI http://my-host.1domain:1234/path
at java.net.http/jdk.internal.net.http.common.Utils.newIAE(Utils.java:326)
at java.net.http/jdk.internal.net.http.HttpRequestBuilderImpl.checkURI(HttpRequestBuilderImpl.java:85)
at java.net.http/jdk.internal.net.http.HttpRequestBuilderImpl.uri(HttpRequestBuilderImpl.java:71)
at java.net.http/jdk.internal.net.http.HttpRequestBuilderImpl.uri(HttpRequestBuilderImpl.java:43)
`
but RestTemplate will success
Comment From: bclozel
I don't think this is a Spring Framework issue. RestClient
uses by default the new java.net.http.HttpClient
, which shows this behavior. You can reproduce the same problem by instantiating the URI manually:
URI uri = new URI("http://my-host.1domain:1234/path");
assertThat(uri.getHost()).isNotNull();
If you think this behavior is incorrect, please raise a JDK issue.
You can choose a different request factory (like the org.springframework.http.client.SimpleClientHttpRequestFactory
):
RestClient client = RestClient.builder().requestFactory(new SimpleClientHttpRequestFactory()).build();
String body = client.get().uri("http://myhost.1domain:" + port + "/test")
.retrieve().body(String.class);
assertThat(body).isEqualTo("test");