How to get locale in Spring WebFlux?
Do not use LocaleContextHolder
in WebFlux environment but add Locale
as a method parameter instead:
@RestController
public class LocaleController {
@GetMapping("/locale")
String getLocale(Locale locale) {
return locale.toLanguageTag();
}
}
Test:
$ curl localhost:8080/locale -H 'Accept-Language: en-US'
en-US
$ curl localhost:8080/locale -H 'Accept-Language: en-GB'
en-GB
For more info see:
- Spring WebFlux. Annotated Controllers. Method Arguments