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

Spring WebFlux - Custom messages validation in different languages #14253

Closed
jakubdyszkiewicz opened this issue Aug 30, 2018 · 2 comments
Closed
Labels
for: external-project For an external project and not something we can fix

Comments

@jakubdyszkiewicz
Copy link

Hello,

I've got a problem with providing custom messages in different languages for bean validation using WebFlux. It does not use messages for locale I send in header.

I think this is a bug because LocalValidatorFactoryBean uses LocaleContextMessageInterpolator. Hibernate Validator calls the public String interpolate(String message, Context context) method, which relays on the LocaleContextHolder. It uses a ThreadLocal which can't be used in WebFlux.

I created sample repository, I hope you find it helpful
https://github.com/jakubdyszkiewicz/webflux-validation

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 30, 2018
@nosan
Copy link
Contributor

nosan commented Aug 30, 2018

Hello @jakubdyszkiewicz ,
WebMvc works because FrameworkServlet has a method FrameworkServlet.initContextHolders to set LocaleContextHolder.setLocaleContext(...). Seems like that WebFlux doesn't have such functionallity at least I haven't found it.

As a workaround you can override a default httpHandler:

@Bean
	@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
	public HttpHandler httpHandler(ApplicationContext applicationContext) {
		HttpHandler delegate = WebHttpHandlerBuilder
				.applicationContext(applicationContext).build();
		return new HttpWebHandlerAdapter(((HttpWebHandlerAdapter) delegate)) {
			@Override
			protected ServerWebExchange createExchange(ServerHttpRequest request,
					ServerHttpResponse response) {
				ServerWebExchange serverWebExchange = super
						.createExchange(request, response);
				LocaleContext localeContext = serverWebExchange.getLocaleContext();
				if (localeContext != null) {
					LocaleContextHolder.setLocaleContext(localeContext);
				}
				return serverWebExchange;
			}
		};
	}

@snicoll
Copy link
Member

snicoll commented Aug 30, 2018

thanks @nosan

@jakubdyszkiewicz this issue is related to WebFlux, the issue tracker of Spring Framework is here. Please report the issue with the sample there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

4 participants