use of org.springframework.web.bind.support.WebBindingInitializer in project spring-framework by spring-projects.
the class ModelInitializerTests method basic.
@SuppressWarnings("unchecked")
@Test
public void basic() throws Exception {
TestController controller = new TestController();
Validator validator = mock(Validator.class);
controller.setValidator(validator);
List<SyncInvocableHandlerMethod> binderMethods = getBinderMethods(controller);
List<InvocableHandlerMethod> attributeMethods = getAttributeMethods(controller);
WebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
BindingContext bindingContext = new InitBinderBindingContext(bindingInitializer, binderMethods);
this.modelInitializer.initModel(bindingContext, attributeMethods, this.exchange).block(Duration.ofMillis(5000));
WebExchangeDataBinder binder = bindingContext.createDataBinder(this.exchange, "name");
assertEquals(Collections.singletonList(validator), binder.getValidators());
Map<String, Object> model = bindingContext.getModel().asMap();
assertEquals(5, model.size());
Object value = model.get("bean");
assertEquals("Bean", ((TestBean) value).getName());
value = model.get("monoBean");
assertEquals("Mono Bean", ((Mono<TestBean>) value).block(Duration.ofMillis(5000)).getName());
value = model.get("singleBean");
assertEquals("Single Bean", ((Single<TestBean>) value).toBlocking().value().getName());
value = model.get("voidMethodBean");
assertEquals("Void Method Bean", ((TestBean) value).getName());
value = model.get("voidMonoMethodBean");
assertEquals("Void Mono Method Bean", ((TestBean) value).getName());
}
use of org.springframework.web.bind.support.WebBindingInitializer in project spring-framework by spring-projects.
the class WebFluxConfigurationSupportTests method requestMappingHandlerAdapter.
@Test
public void requestMappingHandlerAdapter() {
ApplicationContext context = loadConfig(WebFluxConfig.class);
String name = "requestMappingHandlerAdapter";
RequestMappingHandlerAdapter adapter = context.getBean(name, RequestMappingHandlerAdapter.class);
assertThat(adapter).isNotNull();
List<HttpMessageReader<?>> readers = adapter.getMessageReaders();
assertThat(readers.size()).isEqualTo(14);
ResolvableType multiValueMapType = forClassWithGenerics(MultiValueMap.class, String.class, String.class);
assertHasMessageReader(readers, forClass(byte[].class), APPLICATION_OCTET_STREAM);
assertHasMessageReader(readers, forClass(ByteBuffer.class), APPLICATION_OCTET_STREAM);
assertHasMessageReader(readers, forClass(String.class), TEXT_PLAIN);
assertHasMessageReader(readers, forClass(Resource.class), IMAGE_PNG);
assertHasMessageReader(readers, forClass(Message.class), new MediaType("application", "x-protobuf"));
assertHasMessageReader(readers, multiValueMapType, APPLICATION_FORM_URLENCODED);
assertHasMessageReader(readers, forClass(TestBean.class), APPLICATION_XML);
assertHasMessageReader(readers, forClass(TestBean.class), APPLICATION_JSON);
assertHasMessageReader(readers, forClass(TestBean.class), new MediaType("application", "x-jackson-smile"));
assertHasMessageReader(readers, forClass(TestBean.class), null);
WebBindingInitializer bindingInitializer = adapter.getWebBindingInitializer();
assertThat(bindingInitializer).isNotNull();
WebExchangeDataBinder binder = new WebExchangeDataBinder(new Object());
bindingInitializer.initBinder(binder);
name = "webFluxConversionService";
ConversionService service = context.getBean(name, ConversionService.class);
assertThat(binder.getConversionService()).isSameAs(service);
name = "webFluxValidator";
Validator validator = context.getBean(name, Validator.class);
assertThat(binder.getValidator()).isSameAs(validator);
}
use of org.springframework.web.bind.support.WebBindingInitializer in project spring-framework by spring-projects.
the class ModelInitializerTests method getBindingContext.
private InitBinderBindingContext getBindingContext(Object controller) {
List<SyncInvocableHandlerMethod> binderMethods = MethodIntrospector.selectMethods(controller.getClass(), BINDER_METHODS).stream().map(method -> new SyncInvocableHandlerMethod(controller, method)).collect(Collectors.toList());
WebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
return new InitBinderBindingContext(bindingInitializer, binderMethods);
}
Aggregations