Search in sources :

Example 1 with DefaultFormattingConversionService

use of org.springframework.format.support.DefaultFormattingConversionService in project spring-framework by spring-projects.

the class RequestHeaderMethodArgumentResolverTests method dateConversion.

@Test
@SuppressWarnings("deprecation")
public void dateConversion() throws Exception {
    String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
    servletRequest.addHeader("name", rfc1123val);
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setConversionService(new DefaultFormattingConversionService());
    Object result = resolver.resolveArgument(paramDate, null, webRequest, new DefaultDataBinderFactory(bindingInitializer));
    assertTrue(result instanceof Date);
    assertEquals(new Date(rfc1123val), result);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Date(java.util.Date) Test(org.junit.Test)

Example 2 with DefaultFormattingConversionService

use of org.springframework.format.support.DefaultFormattingConversionService in project spring-framework by spring-projects.

the class InitBinderDataBinderFactoryTests method createBinderWithGlobalInitialization.

@Test
public void createBinderWithGlobalInitialization() throws Exception {
    ConversionService conversionService = new DefaultFormattingConversionService();
    bindingInitializer.setConversionService(conversionService);
    WebDataBinderFactory factory = createFactory("initBinder", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);
    assertSame(conversionService, dataBinder.getConversionService());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) ConversionService(org.springframework.core.convert.ConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 3 with DefaultFormattingConversionService

use of org.springframework.format.support.DefaultFormattingConversionService in project spring-framework by spring-projects.

the class WebFluxConfigurationSupport method webFluxConversionService.

@Bean
public FormattingConversionService webFluxConversionService() {
    FormattingConversionService service = new DefaultFormattingConversionService();
    addFormatters(service);
    return service;
}
Also used : FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Bean(org.springframework.context.annotation.Bean)

Example 4 with DefaultFormattingConversionService

use of org.springframework.format.support.DefaultFormattingConversionService 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 5 with DefaultFormattingConversionService

use of org.springframework.format.support.DefaultFormattingConversionService 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)

Aggregations

DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)16 Test (org.junit.Test)8 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)7 BindingContext (org.springframework.web.reactive.BindingContext)6 Before (org.junit.Before)4 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)3 FormattingConversionService (org.springframework.format.support.FormattingConversionService)3 Optional (java.util.Optional)2 Bean (org.springframework.context.annotation.Bean)2 MethodParameter (org.springframework.core.MethodParameter)2 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)2 ConversionService (org.springframework.core.convert.ConversionService)2 WebDataBinder (org.springframework.web.bind.WebDataBinder)2 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)2 Method (java.lang.reflect.Method)1 Instant (java.time.Instant)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 CasConfigurationEmbeddedValueResolver (org.apereo.cas.config.support.CasConfigurationEmbeddedValueResolver)1