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

Use WebClientCustomizer to create WebClients #48

Open
sharpedavid opened this issue Dec 16, 2020 · 0 comments
Open

Use WebClientCustomizer to create WebClients #48

sharpedavid opened this issue Dec 16, 2020 · 0 comments

Comments

@sharpedavid
Copy link
Contributor

We are creating our WebClient's using the base API that overrides Spring Boot's defaults. This was not an intentional choice; it's just the first thing we tried.

WebClient Customization

There are three main approaches to WebClient customization, depending on how broadly you want the customizations to apply.

(1) To make the scope of any customizations as narrow as possible, inject the auto-configured WebClient.Builder and then call its methods as required. WebClient.Builder instances are stateful: Any change on the builder is reflected in all clients subsequently created with it. If you want to create several clients with the same builder, you can also consider cloning the builder with WebClient.Builder other = builder.clone();.

(2) To make an application-wide, additive customization to all WebClient.Builder instances, you can declare WebClientCustomizer beans and change the WebClient.Builder locally at the point of injection.

(3) Finally, you can fall back to the original API and use WebClient.create(). In that case, no auto-configuration or WebClientCustomizer is applied.

We are currently using option 3, like this:

We should be using option 2, like this:

@Bean
public WebClientCustomizer webClientCustomizer(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
	return webClientBuilder -> webClientBuilder
		.filter(new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager));
}

Reason to make the change

Quoting this comment:

I realized that if you use spring boot you should not define the WebClient bean like this [option 3]. Spring boot normally configures a WebClient.Builder instance that considers settings like spring.jackson.serialization.write-dates-as-timestamps=false etc. and creates a WebClient bean for you, using this builder. But if you create a WebClient bean like this [option 3], the settings in spring boot’s builder are lost - and so are you! 😉

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

No branches or pull requests

1 participant