Search in sources :

Example 1 with WebBindingInitializer

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());
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Mono(reactor.core.publisher.Mono) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) BindingContext(org.springframework.web.reactive.BindingContext) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Validator(org.springframework.validation.Validator) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Test(org.junit.Test)

Example 2 with WebBindingInitializer

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);
}
Also used : Message(com.google.protobuf.Message) Resource(org.springframework.core.io.Resource) ByteBuffer(java.nio.ByteBuffer) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConversionService(org.springframework.core.convert.ConversionService) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Validator(org.springframework.validation.Validator) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) Test(org.junit.jupiter.api.Test)

Example 3 with WebBindingInitializer

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);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Single(io.reactivex.rxjava3.core.Single) Validator(org.springframework.validation.Validator) MethodIntrospector(org.springframework.core.MethodIntrospector) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethod(org.springframework.web.method.HandlerMethod) Model(org.springframework.ui.Model) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) SessionAttributes(org.springframework.web.bind.annotation.SessionAttributes) Duration(java.time.Duration) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) Nullable(org.springframework.lang.Nullable) WebSession(org.springframework.web.server.WebSession) Method(java.lang.reflect.Method) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) PostMapping(org.springframework.web.bind.annotation.PostMapping) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ResolvableMethod(org.springframework.web.testfixture.method.ResolvableMethod) ReflectionUtils(org.springframework.util.ReflectionUtils) WebDataBinder(org.springframework.web.bind.WebDataBinder) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) InitBinder(org.springframework.web.bind.annotation.InitBinder) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer)

Aggregations

Validator (org.springframework.validation.Validator)3 WebBindingInitializer (org.springframework.web.bind.support.WebBindingInitializer)3 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)3 Test (org.junit.jupiter.api.Test)2 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)2 SyncInvocableHandlerMethod (org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)2 Mono (reactor.core.publisher.Mono)2 Message (com.google.protobuf.Message)1 Single (io.reactivex.rxjava3.core.Single)1 Method (java.lang.reflect.Method)1 ByteBuffer (java.nio.ByteBuffer)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 Test (org.junit.Test)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1