Search in sources :

Example 6 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class PathVariableMethodArgumentResolverTests method resolveArgumentWrappedAsOptional.

@Test
public void resolveArgumentWrappedAsOptional() throws Exception {
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "value");
    this.exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    BindingContext bindingContext = new BindingContext(initializer);
    Mono<Object> mono = this.resolver.resolveArgument(this.paramOptional, bindingContext, this.exchange);
    Object result = mono.block();
    assertEquals(Optional.of("value"), result);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) HashMap(java.util.HashMap) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 7 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class RequestAttributeMethodArgumentResolverTests method resolveOptional.

@Test
public void resolveOptional() throws Exception {
    MethodParameter param = initMethodParameter(3);
    Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    assertFalse(((Optional<?>) mono.block()).isPresent());
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    BindingContext bindingContext = new BindingContext(initializer);
    Foo foo = new Foo();
    this.exchange.getAttributes().put("foo", foo);
    mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    Optional<?> optional = (Optional<?>) mono.block();
    assertTrue(optional.isPresent());
    assertSame(foo, optional.get());
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Optional(java.util.Optional) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 8 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class RequestHeaderMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.refresh();
    ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
    this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    this.bindingContext = new BindingContext(initializer);
    Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
    this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
    this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
    this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
    this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
    this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
    this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
    this.paramDate = new SynthesizingMethodParameter(method, 6);
    this.paramInstant = new SynthesizingMethodParameter(method, 7);
    this.paramMono = new SynthesizingMethodParameter(method, 8);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) Method(java.lang.reflect.Method) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Before(org.junit.Before)

Example 9 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method getConfigurableWebBindingInitializer.

/**
	 * 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;
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer)

Example 10 with ConfigurableWebBindingInitializer

use of org.springframework.web.bind.support.ConfigurableWebBindingInitializer in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() throws Exception {
    ApplicationContext context = initContext(WebConfig.class);
    RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
    List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
    assertEquals(11, converters.size());
    converters.stream().filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter).forEach(converter -> {
        ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
        assertFalse(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
        assertFalse(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
        assertFalse(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
        if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
            assertEquals(XmlMapper.class, mapper.getClass());
        }
    });
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    assertNotNull(initializer);
    ConversionService conversionService = initializer.getConversionService();
    assertNotNull(conversionService);
    assertTrue(conversionService instanceof FormattingConversionService);
    Validator validator = initializer.getValidator();
    assertNotNull(validator);
    assertTrue(validator instanceof LocalValidatorFactoryBean);
    DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
    @SuppressWarnings("unchecked") List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
    assertEquals(2, bodyAdvice.size());
    assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
    assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PathVariable(org.springframework.web.bind.annotation.PathVariable) DEFAULT_VIEW_INCLUSION(com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION) Validator(org.springframework.validation.Validator) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) FAIL_ON_UNKNOWN_PROPERTIES(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Locale(java.util.Locale) MethodParameter(org.springframework.core.MethodParameter) AntPathMatcher(org.springframework.util.AntPathMatcher) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Configuration(org.springframework.context.annotation.Configuration) MockServletContext(org.springframework.mock.web.test.MockServletContext) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Assert.assertFalse(org.junit.Assert.assertFalse) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) Ordered(org.springframework.core.Ordered) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Controller(org.springframework.stereotype.Controller) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Scope(org.springframework.context.annotation.Scope) HttpServletRequest(javax.servlet.http.HttpServletRequest) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ConversionService(org.springframework.core.convert.ConversionService) MessageSource(org.springframework.context.MessageSource) ViewResolver(org.springframework.web.servlet.ViewResolver) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) PathMatcher(org.springframework.util.PathMatcher) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) ScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FormattingConversionService(org.springframework.format.support.FormattingConversionService) ApplicationContext(org.springframework.context.ApplicationContext) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) HttpStatus(org.springframework.http.HttpStatus) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) Assert.assertNull(org.junit.Assert.assertNull) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Bean(org.springframework.context.annotation.Bean) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) Assert.assertEquals(org.junit.Assert.assertEquals) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) FormattingConversionService(org.springframework.format.support.FormattingConversionService) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) ConversionService(org.springframework.core.convert.ConversionService) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) List(java.util.List) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Aggregations

ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)26 Test (org.junit.Test)17 MethodParameter (org.springframework.core.MethodParameter)8 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)8 BindingContext (org.springframework.web.reactive.BindingContext)8 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)7 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)7 Before (org.junit.Before)6 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)6 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)5 Optional (java.util.Optional)4 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)4 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)3 ConversionService (org.springframework.core.convert.ConversionService)3 RequestParam (org.springframework.web.bind.annotation.RequestParam)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 Bean (org.springframework.context.annotation.Bean)2