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

Creating a RestClient from a RestTemplateBuilder-created RestTemplate requires double configuration of the baseUrl/rootUri #39317

Closed
wilkinsona opened this issue Jan 26, 2024 · 1 comment
Assignees
Labels
type: bug A general bug
Milestone

Comments

@wilkinsona
Copy link
Member

You have do this:

RestClient rest = RestClient
	.builder(builder.setConnectTimeout(Duration.ofSeconds(30))
		.setReadTimeout(Duration.ofSeconds(30))
		.rootUri("https://example.com)
		.build())
	.defaultHeader("Authorization", "Bearer " + properties.token())
	.baseUrl("https://example.com")
	.build();

If you only configure rootUri on the RestTemplateBuilder, it's ignored by RestClient.Builder as the built RestTemplate has a UriTemplateHandler that isn't a UriBuilderFactory. If you only configure baseUrl on the RestClient.Builder it's ignored as the UriBuilderFactory from the RestTemplate is used instead. Configuring both prevents the UriBuilderFactory from the RestTemplate from being used which then allows the baseUrl on the RestClient.Builder to be honored.

@wilkinsona wilkinsona added the type: bug A general bug label Jan 26, 2024
@wilkinsona wilkinsona added this to the 3.2.x milestone Jan 26, 2024
@scottfrederick scottfrederick self-assigned this Feb 1, 2024
@scottfrederick
Copy link
Contributor

If you only configure rootUri on the RestTemplateBuilder, it's ignored by RestClient.Builder as the built RestTemplate has a UriTemplateHandler that isn't a UriBuilderFactory.

This can be fixed by making the RootUriTemplateHandler used by RestTemplateBuilder extend Framework's DefaultUriBuilderFactory.

If you only configure baseUrl on the RestClient.Builder it's ignored as the UriBuilderFactory from the RestTemplate is used instead.

This was a problem in Framework that has been fixed by spring-projects/spring-framework#32180.

Configuring both prevents the UriBuilderFactory from the RestTemplate from being used which then allows the baseUrl on the RestClient.Builder to be honored.

After the fix for the first scenario, the UriBuilderFactory from the RestTemplate will be used instead of the RestClient.Builder.baseUrl() value. This decision is made in Framework code, and is out of Spring Boot's control. You'll get the same behavior if you create a RestTemplate and call setUriTemplateHandler on it without using Boot's RestTemplateBuilder.

I have a test that verifies all three scenarios, but the second test case won't pass until we move to Spring Framework 6.1.4 snapshots. I'll wait until then to push the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants