Search in sources :

Example 1 with ConversionService

use of org.springframework.core.convert.ConversionService in project spring-boot by spring-projects.

the class SpringApplication method bindToSpringApplication.

/**
	 * Bind the environment to the {@link SpringApplication}.
	 * @param environment the environment to bind
	 */
protected void bindToSpringApplication(ConfigurableEnvironment environment) {
    PropertiesConfigurationFactory<SpringApplication> binder = new PropertiesConfigurationFactory<>(this);
    ConversionService conversionService = new DefaultConversionService();
    binder.setTargetName("spring.main");
    binder.setConversionService(conversionService);
    binder.setPropertySources(environment.getPropertySources());
    try {
        binder.bindPropertiesToTarget();
    } catch (BindException ex) {
        throw new IllegalStateException("Cannot bind to SpringApplication", ex);
    }
}
Also used : PropertiesConfigurationFactory(org.springframework.boot.bind.PropertiesConfigurationFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConversionService(org.springframework.core.convert.ConversionService) BindException(org.springframework.validation.BindException) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService)

Example 2 with ConversionService

use of org.springframework.core.convert.ConversionService in project spring-framework by spring-projects.

the class ConversionServiceFactoryBeanTests method createDefaultConversionService.

@Test
public void createDefaultConversionService() {
    ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    factory.afterPropertiesSet();
    ConversionService service = factory.getObject();
    assertTrue(service.canConvert(String.class, Integer.class));
}
Also used : ConversionService(org.springframework.core.convert.ConversionService) Test(org.junit.Test)

Example 3 with ConversionService

use of org.springframework.core.convert.ConversionService in project spring-framework by spring-projects.

the class DefaultConversionService method addCollectionConverters.

/**
	 * Add collection converters.
	 * @param converterRegistry the registry of converters to add to (must also be castable to ConversionService,
	 * e.g. being a {@link ConfigurableConversionService})
	 * @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService
	 * @since 4.2.3
	 */
public static void addCollectionConverters(ConverterRegistry converterRegistry) {
    ConversionService conversionService = (ConversionService) converterRegistry;
    converterRegistry.addConverter(new ArrayToCollectionConverter(conversionService));
    converterRegistry.addConverter(new CollectionToArrayConverter(conversionService));
    converterRegistry.addConverter(new ArrayToArrayConverter(conversionService));
    converterRegistry.addConverter(new CollectionToCollectionConverter(conversionService));
    converterRegistry.addConverter(new MapToMapConverter(conversionService));
    converterRegistry.addConverter(new ArrayToStringConverter(conversionService));
    converterRegistry.addConverter(new StringToArrayConverter(conversionService));
    converterRegistry.addConverter(new ArrayToObjectConverter(conversionService));
    converterRegistry.addConverter(new ObjectToArrayConverter(conversionService));
    converterRegistry.addConverter(new CollectionToStringConverter(conversionService));
    converterRegistry.addConverter(new StringToCollectionConverter(conversionService));
    converterRegistry.addConverter(new CollectionToObjectConverter(conversionService));
    converterRegistry.addConverter(new ObjectToCollectionConverter(conversionService));
    converterRegistry.addConverter(new StreamConverter(conversionService));
}
Also used : ConversionService(org.springframework.core.convert.ConversionService)

Example 4 with ConversionService

use of org.springframework.core.convert.ConversionService in project spring-framework by spring-projects.

the class MediaTypeTests method testWithConversionService.

@Test
public void testWithConversionService() {
    ConversionService conversionService = new DefaultConversionService();
    assertTrue(conversionService.canConvert(String.class, MediaType.class));
    MediaType mediaType = MediaType.parseMediaType("application/xml");
    assertEquals(mediaType, conversionService.convert("application/xml", MediaType.class));
}
Also used : ConversionService(org.springframework.core.convert.ConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Example 5 with ConversionService

use of org.springframework.core.convert.ConversionService 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)

Aggregations

ConversionService (org.springframework.core.convert.ConversionService)21 Test (org.junit.Test)11 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)5 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)4 ApplicationContext (org.springframework.context.ApplicationContext)3 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)3 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)3 Collection (java.util.Collection)2 Map (java.util.Map)2 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)2 FormattingConversionService (org.springframework.format.support.FormattingConversionService)2 Validator (org.springframework.validation.Validator)2 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)2 RequestMappingHandlerAdapter (org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter)2 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)2 FAIL_ON_UNKNOWN_PROPERTIES (com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)1 DEFAULT_VIEW_INCLUSION (com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION)1