/** * Create an instance from a collection of {@link UriComponentsContributor UriComponentsContributors} or * {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}. Since both of these tend to be implemented * by the same class, the most convenient option is to obtain the configured * {@code HandlerMethodArgumentResolvers} in {@code RequestMappingHandlerAdapter} * and provide that to this constructor. * @param contributors a collection of {@link UriComponentsContributor} * or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}. */ public CompositeUriComponentsContributor(UriComponentsContributor... contributors) { Collections.addAll(this.contributors, contributors); this.conversionService = new DefaultFormattingConversionService(); }
/** * Return a {@link FormattingConversionService} for use with annotated controllers. * <p>See {@link #addFormatters} as an alternative to overriding this method. */ @Bean public FormattingConversionService webFluxConversionService() { FormattingConversionService service = new DefaultFormattingConversionService(); addFormatters(service); return service; }
/** * Return a {@link FormattingConversionService} for use with annotated controllers. * <p>See {@link #addFormatters} as an alternative to overriding this method. */ @Bean public FormattingConversionService mvcConversionService() { FormattingConversionService conversionService = new DefaultFormattingConversionService(); addFormatters(conversionService); return conversionService; }
/** * Create an instance from a collection of {@link UriComponentsContributor UriComponentsContributors} or * {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}. Since both of these tend to be implemented * by the same class, the most convenient option is to obtain the configured * {@code HandlerMethodArgumentResolvers} in the {@code RequestMappingHandlerAdapter} * and provide that to this constructor. * <p>If the {@link ConversionService} argument is {@code null}, * {@link org.springframework.format.support.DefaultFormattingConversionService} * will be used by default. * @param contributors a collection of {@link UriComponentsContributor} * or {@link HandlerMethodArgumentResolver HandlerMethodArgumentResolvers}. * @param cs a ConversionService to use when method argument values * need to be formatted as Strings before being added to the URI */ public CompositeUriComponentsContributor(@Nullable Collection<?> contributors, @Nullable ConversionService cs) { if (contributors != null) { this.contributors.addAll(contributors); } this.conversionService = (cs != null ? cs : new DefaultFormattingConversionService()); }
@Override public void afterPropertiesSet() { this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); registerFormatters(this.conversionService); }
/** * Return a {@link FormattingConversionService} for use with annotated controllers. * <p>See {@link #addFormatters} as an alternative to overriding this method. */ @Bean public FormattingConversionService mvcConversionService() { FormattingConversionService conversionService = new DefaultFormattingConversionService(); addFormatters(conversionService); return conversionService; }
@Override public void afterPropertiesSet() { this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); registerFormatters(this.conversionService); }
@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); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.initDirectFieldAccess(); binder.setConversionService(new DefaultFormattingConversionService()); }
@Before public void setup() { this.conversionService = new DefaultFormattingConversionService(); DataBinder dataBinder = new DataBinder(null); dataBinder.setConversionService(conversionService); this.redirectAttributes = new RedirectAttributesModelMap(dataBinder); }
@Test public void resolveOptional() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().name("foo")).arg(Optional.class, Foo.class); 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()); }
@SuppressWarnings("unchecked") @Test public void resolveOptional() { MethodParameter param = initMethodParameter(3); Optional<Object> actual = (Optional<Object>) this.resolver .resolveArgument(param, new BindingContext(), this.exchange).block(); assertNotNull(actual); assertFalse(actual.isPresent()); ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultFormattingConversionService()); BindingContext bindingContext = new BindingContext(initializer); Foo foo = new Foo(); when(this.session.getAttribute("foo")).thenReturn(foo); actual = (Optional<Object>) this.resolver.resolveArgument(param, bindingContext, this.exchange).block(); assertNotNull(actual); assertTrue(actual.isPresent()); assertSame(foo, actual.get()); }
@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); }
@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()); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.initDirectFieldAccess(); binder.setConversionService(new DefaultFormattingConversionService()); LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean(); vf.afterPropertiesSet(); binder.setValidator(vf); }
@Before public void setup() throws Exception { ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); this.resolver = new RequestParamMethodArgumentResolver(null, adapterRegistry, true); ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultFormattingConversionService()); this.bindContext = new BindingContext(initializer); }
@Test public void instantConversion() 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(paramInstant, null, webRequest, new DefaultDataBinderFactory(bindingInitializer)); assertTrue(result instanceof Instant); assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result); }
@Test public void createBinderWithGlobalInitialization() throws Exception { ConversionService conversionService = new DefaultFormattingConversionService(); bindingInitializer.setConversionService(conversionService); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); BindingContext context = createBindingContext("initBinder", WebDataBinder.class); WebDataBinder dataBinder = context.createDataBinder(exchange, null, null); assertSame(conversionService, dataBinder.getConversionService()); }
@Before @SuppressWarnings("resource") public void setup() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.refresh(); ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); 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); }
@Test public void testConversionWithInappropriateStringEditor() { DataBinder dataBinder = new DataBinder(null); DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); dataBinder.setConversionService(conversionService); dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); NameBean bean = new NameBean("Fred"); assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class)); conversionService.addConverter(new NameBeanConverter()); assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class)); }