I added the following @Bean in the classpath for overriding default LocaleContextResolver in spring webflux
@Configuration
public class LocaleResolverConfiguration {
@Bean(WebHttpHandlerBuilder.LOCALE_CONTEXT_RESOLVER_BEAN_NAME)
public LocaleContextResolver localeContextResolver() {
return new LocaleContextResolver() {
@Override
public LocaleContext resolveLocaleContext(ServerWebExchange exchange) {
final String langParam = exchange.getRequest().getQueryParams().getFirst("lang");
if (langParam == null) {
return new SimpleLocaleContext(Locale.getDefault());
} else {
return new SimpleLocaleContext(Locale.forLanguageTag(langParam));
}
}
@Override
public void setLocaleContext(ServerWebExchange exchange, LocaleContext localeContext) {
throw new UnsupportedOperationException(
"Cannot change HTTP accept header - use a different locale context resolution strategy");
}
};
}
}
But the application startup fails with the following error
The bean 'localeContextResolver', defined in class path resource [org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration$EnableWebFluxConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/acme/webflux/config/LocaleResolverWebFluxConfiguration.class] and overriding is disabled.
Why is webflux not backing off when we already provided our own LocaleContextResolver localeContextResolver()?
Is there some other way to achieve the same thing?