Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsist baseurl behaviour between RestClient and WebClient #32185

Closed
wimdeblauwe opened this issue Feb 1, 2024 · 2 comments
Closed

Inconsist baseurl behaviour between RestClient and WebClient #32185

wimdeblauwe opened this issue Feb 1, 2024 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@wimdeblauwe
Copy link

I migrated some code that uses the declarative http interface support of Spring from WebClient to RestClient as the underlying implementation. Our code started failing on the staging server after that. I managed to trace the problem to a difference in handling of the base url between RestClient and WebClient.

The difference is easily seen via this test program:

import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class Test {

  public static void main(String[] args) throws InterruptedException {
    WebClient webClient = WebClient.builder()
        .baseUrl("numbersapi.com").build();

    Mono<String> bodilessEntity = webClient.get().uri("/42").retrieve().bodyToMono(String.class);
    String block = bodilessEntity.block();
    System.out.println("block = " + block);

    RestClient restClient = RestClient.builder()
        .baseUrl("numbersapi.com")
        .build();
    String viaRestClient = restClient.get().uri("/42").retrieve().body(String.class);
    System.out.println("viaRestClient = " + viaRestClient);
  }
}

Note how the base url does not specify a scheme (http or https) in both cases. However, with WebClient, this is not an issue and the call to the remote service is done. With RestClient, an exception is thrown:

Exception in thread "main" java.lang.IllegalArgumentException: URI with undefined scheme
	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:79)
	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)
	at org.springframework.http.client.JdkClientHttpRequest.buildRequest(JdkClientHttpRequest.java:136)
	at org.springframework.http.client.JdkClientHttpRequest.executeInternal(JdkClientHttpRequest.java:95)
	at org.springframework.http.client.AbstractStreamingClientHttpRequest.executeInternal(AbstractStreamingClientHttpRequest.java:70)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66)
	at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchangeInternal(DefaultRestClient.java:468)
	at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.retrieve(DefaultRestClient.java:439)

Maybe it should not have worked in the first place with WebClient, I don't know. But maybe it would be good to have the same behaviour?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 1, 2024
@bclozel bclozel added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Feb 2, 2024
@bclozel
Copy link
Member

bclozel commented Feb 2, 2024

Note: we had reports saying the opposite for WebClient in #31033.

@poutsma poutsma self-assigned this Feb 6, 2024
@poutsma poutsma removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 12, 2024
@poutsma
Copy link
Contributor

poutsma commented Feb 12, 2024

This is a consequence of the fact that WebClient uses Reactor Netty by default, which allows for non-absolute URIs, and that RestClient uses the JDK HttpClient by default, which does not accept non-absolute URIs.

Even if we would fix this in RestClient, for instance by setting a scheme if not present, we would still not be in the same situation as WebClient, because we have do not make a similar check there; we simple pass it to Reactor Netty.

@poutsma poutsma closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2024
@poutsma poutsma added the status: declined A suggestion or change that we don't feel we should currently apply label Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants