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

Unable to use baseUrl on builders created from RestTemplate #32180

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

Unable to use baseUrl on builders created from RestTemplate #32180

tzolov opened this issue Feb 1, 2024 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@tzolov
Copy link

tzolov commented Feb 1, 2024

Affects: \6.1.3


If RestClient Builder is created from a RestTemplate an attempt to set a the baseUrl would leads to the java.lang.IllegalArgumentException: URI is not absolute exception.

To reproduce:

@Test
public void test2() {
  String baseUrl = "https://dog.ceo";
  RestTemplate restTemplate = new RestTemplate();
  var restClient = RestClient.builder(restTemplate).baseUrl(baseUrl).build();
 String res = restClient.get().uri("/api/breeds/list/all").retrieve().body(String.class);
 assertThat(res).isNotNull();
}

would throw:

java.lang.IllegalArgumentException: URI is not absolute
 at java.base/java.net.URL.fromURI([URL.java:721](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.html))
 at java.base/java.net.URI.toURL([URI.java:1139](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.html))
 at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest([SimpleClientHttpRequestFactory.java:154](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.html))
 at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.createRequest([DefaultRestClient.java:528](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.html))

Note: If you set the restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(baseUrl)); before creating the builder it won't fail.

@poutsma poutsma self-assigned this Feb 1, 2024
@poutsma poutsma added status: waiting-for-triage An issue we've not yet triaged or decided on in: web Issues in web modules (web, webmvc, webflux, websocket) labels Feb 1, 2024
@Akaame
Copy link
Contributor

Akaame commented Feb 1, 2024

@poutsma I reproduced this and what happens is clear:

private UriBuilderFactory initUriBuilderFactory() {
if (this.uriBuilderFactory != null) {
	return this.uriBuilderFactory;
}
DefaultUriBuilderFactory factory = (this.baseUrl != null ?
		new DefaultUriBuilderFactory(this.baseUrl) : new DefaultUriBuilderFactory());
factory.setDefaultUriVariables(this.defaultUriVariables);
return factory;
}

The condition here is not respected because of the DefaultUriBuilderFactory. This is what I am seeing:

image

Therefore the baseUrl is never respected. The code works when the setUriTemplateHandler is called with a UriBuilderFactory explicitly because it creates instances with baseUrl already. I have not contributed Spring enough to suggest the best solution here. Maybe it is advisable to ensure either the existence of this.uriBuilderFactory.baseUrl or the this.baseUrl itself.

@poutsma poutsma added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 2, 2024
@poutsma poutsma added this to the 6.1.4 milestone Feb 2, 2024
@poutsma poutsma changed the title Unable to use the RestClient.Builder.baseUrl() for builders created from a RestTemplate Unable to use baseUrl on builders created from RestTemplate Feb 2, 2024
@poutsma poutsma closed this as completed in e15c150 Feb 2, 2024
@jhoeller
Copy link
Contributor

jhoeller commented Feb 6, 2024

@poutsma I'm replacing hasRestTemplateDefaults() with hasBaseUri() in DefaultUriBuilderFactory since this allows for keeping the actual RestTemplate-style settings check in DefaultRestClientBuilder itself where it conceptually fits better.

Not least of it all, RestClient and RestTemplate live in the web.client package whereas DefaultUriBuilderFactory lives in web.util where it shouldn't logically depend on RestTemplate and its default settings, in particular not in a public method name.

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) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants