Add WebExchangeDataBinder
Issue: SPR-14541
This commit is contained in:
@@ -55,7 +55,10 @@ import org.springframework.http.codec.xml.Jaxb2XmlDecoder;
|
||||
import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.MessageCodesResolver;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.accept.CompositeContentTypeResolver;
|
||||
@@ -255,6 +258,7 @@ public class WebReactiveConfiguration implements ApplicationContextAware {
|
||||
}
|
||||
|
||||
adapter.setMessageReaders(getMessageReaders());
|
||||
adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
|
||||
adapter.setConversionService(mvcConversionService());
|
||||
adapter.setValidator(mvcValidator());
|
||||
|
||||
@@ -325,6 +329,18 @@ public class WebReactiveConfiguration implements ApplicationContextAware {
|
||||
protected void extendMessageReaders(List<HttpMessageReader<?>> messageReaders) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link ConfigurableWebBindingInitializer} to use for
|
||||
* initializing all {@link WebDataBinder} instances.
|
||||
*/
|
||||
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
|
||||
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
|
||||
initializer.setConversionService(mvcConversionService());
|
||||
initializer.setValidator(mvcValidator());
|
||||
initializer.setMessageCodesResolver(getMessageCodesResolver());
|
||||
return initializer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FormattingConversionService mvcConversionService() {
|
||||
FormattingConversionService service = new DefaultFormattingConversionService();
|
||||
@@ -378,6 +394,13 @@ public class WebReactiveConfiguration implements ApplicationContextAware {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to provide a custom {@link MessageCodesResolver}.
|
||||
*/
|
||||
protected MessageCodesResolver getMessageCodesResolver() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleHandlerAdapter simpleHandlerAdapter() {
|
||||
return new SimpleHandlerAdapter();
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.web.bind.support.WebBindingInitializer;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.method.annotation.ExceptionHandlerMethodResolver;
|
||||
import org.springframework.web.reactive.HandlerAdapter;
|
||||
@@ -68,6 +69,8 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
|
||||
|
||||
private ReactiveAdapterRegistry reactiveAdapters = new ReactiveAdapterRegistry();
|
||||
|
||||
private WebBindingInitializer webBindingInitializer;
|
||||
|
||||
private ConversionService conversionService = new DefaultFormattingConversionService();
|
||||
|
||||
private Validator validator;
|
||||
@@ -136,6 +139,21 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
|
||||
return this.reactiveAdapters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a WebBindingInitializer with "global" initialization to apply
|
||||
* to every DataBinder instance.
|
||||
*/
|
||||
public void setWebBindingInitializer(WebBindingInitializer webBindingInitializer) {
|
||||
this.webBindingInitializer = webBindingInitializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured WebBindingInitializer, or {@code null} if none.
|
||||
*/
|
||||
public WebBindingInitializer getWebBindingInitializer() {
|
||||
return this.webBindingInitializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a ConversionService for type conversion of controller method
|
||||
* arguments as well as for converting from different async types to
|
||||
|
||||
@@ -49,6 +49,8 @@ import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.WebExchangeDataBinder;
|
||||
import org.springframework.web.bind.support.WebBindingInitializer;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
import org.springframework.web.reactive.handler.AbstractHandlerMapping;
|
||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||
@@ -161,6 +163,13 @@ public class WebReactiveConfigurationTests {
|
||||
Validator validator = context.getBean(name, Validator.class);
|
||||
assertSame(validator, adapter.getValidator());
|
||||
assertEquals(OptionalValidatorFactoryBean.class, validator.getClass());
|
||||
|
||||
WebBindingInitializer bindingInitializer = adapter.getWebBindingInitializer();
|
||||
assertNotNull(bindingInitializer);
|
||||
WebExchangeDataBinder binder = new WebExchangeDataBinder(new Object());
|
||||
bindingInitializer.initBinder(binder);
|
||||
assertSame(service, binder.getConversionService());
|
||||
assertSame(validator, binder.getValidator());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user