Java Code Examples for org.springframework.core.convert.support.DefaultConversionService#addConverter()

The following examples show how to use org.springframework.core.convert.support.DefaultConversionService#addConverter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleColumnRowMapperTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-16483
public void useCustomConversionService() throws SQLException {
	Timestamp timestamp = new Timestamp(0);

	DefaultConversionService myConversionService = new DefaultConversionService();
	myConversionService.addConverter(Timestamp.class, MyLocalDateTime.class,
			source -> new MyLocalDateTime(source.toLocalDateTime()));
	SingleColumnRowMapper<MyLocalDateTime> rowMapper =
			SingleColumnRowMapper.newInstance(MyLocalDateTime.class, myConversionService);

	ResultSet resultSet = mock(ResultSet.class);
	ResultSetMetaData metaData = mock(ResultSetMetaData.class);
	given(metaData.getColumnCount()).willReturn(1);
	given(resultSet.getMetaData()).willReturn(metaData);
	given(resultSet.getObject(1, MyLocalDateTime.class))
			.willThrow(new SQLFeatureNotSupportedException());
	given(resultSet.getObject(1)).willReturn(timestamp);

	MyLocalDateTime actualMyLocalDateTime = rowMapper.mapRow(resultSet, 1);

	assertNotNull(actualMyLocalDateTime);
	assertEquals(timestamp.toLocalDateTime(), actualMyLocalDateTime.value);
}
 
Example 2
Source File: AttributeServiceConfiguration.java    From radman with MIT License 6 votes vote down vote up
@Autowired
public AttributeServiceConfiguration(RadCheckAttributeRepo checkAttributeRepo,
                                     RadReplyAttributeRepo replyAttributeRepo,
                                     RadCheckRepo radCheckRepo,
                                     RadReplyRepo radReplyRepo,
                                     RadGroupCheckRepo radGroupCheckRepo,
                                     RadGroupReplyRepo radGroupReplyRepo,
                                     DefaultConversionService conversionService) {
    this.checkAttributeRepo = checkAttributeRepo;
    this.replyAttributeRepo = replyAttributeRepo;
    this.radCheckRepo = radCheckRepo;
    this.radReplyRepo = radReplyRepo;
    this.radGroupCheckRepo = radGroupCheckRepo;
    this.radGroupReplyRepo = radGroupReplyRepo;
    this.conversionService = conversionService;

    conversionService.addConverter(new RadCheckAttributeToDtoConverter());
    conversionService.addConverter(new DtoToRadCheckAttributeConverter());
    conversionService.addConverter(new RadReplyAttributeToDtoConverter());
    conversionService.addConverter(new DtoToRadReplyAttributeConverter());
}
 
Example 3
Source File: RadiusUserServiceConfiguration.java    From radman with MIT License 6 votes vote down vote up
@Autowired
public RadiusUserServiceConfiguration(RadiusUserRepo radiusUserRepo,
                                      RadiusGroupRepo radiusGroupRepo,
                                      RadUserGroupRepo radUserGroupRepo,
                                      RadCheckRepo radCheckRepo,
                                      RadReplyRepo radReplyRepo,
                                      RadGroupCheckRepo radGroupCheckRepo,
                                      RadGroupReplyRepo radGroupReplyRepo,
                                      DefaultConversionService conversionService) {
    this.radiusUserRepo = radiusUserRepo;
    this.radiusGroupRepo = radiusGroupRepo;
    this.radUserGroupRepo = radUserGroupRepo;
    this.radCheckRepo = radCheckRepo;
    this.radReplyRepo = radReplyRepo;
    this.radGroupCheckRepo = radGroupCheckRepo;
    this.radGroupReplyRepo = radGroupReplyRepo;
    this.conversionService = conversionService;

    conversionService.addConverter(new RadiusUserToDtoConverter());
    conversionService.addConverter(new DtoToRadiusUserConverter());
    conversionService.addConverter(new RadiusGroupToDtoConverter());
    conversionService.addConverter(new DtoToRadiusGroupConverter());
    conversionService.addConverter(new RadUserGroupToDtoConverter());
    conversionService.addConverter(new DtoToRadUserGroupConverter());
}
 
Example 4
Source File: AuthServiceConfiguration.java    From radman with MIT License 6 votes vote down vote up
@Autowired
public AuthServiceConfiguration(RadCheckRepo radCheckRepo,
                                RadReplyRepo radReplyRepo,
                                RadGroupCheckRepo radGroupCheckRepo,
                                RadGroupReplyRepo radGroupReplyRepo,
                                RadCheckAttributeRepo radCheckAttributeRepo,
                                RadReplyAttributeRepo radReplyAttributeRepo,
                                DefaultConversionService conversionService) {
    this.radCheckRepo = radCheckRepo;
    this.radReplyRepo = radReplyRepo;
    this.radGroupCheckRepo = radGroupCheckRepo;
    this.radGroupReplyRepo = radGroupReplyRepo;
    this.radCheckAttributeRepo = radCheckAttributeRepo;
    this.radReplyAttributeRepo = radReplyAttributeRepo;
    this.conversionService = conversionService;

    conversionService.addConverter(new DtoToRadCheckConverter());
    conversionService.addConverter(new DtoToRadReplyConverter());
    conversionService.addConverter(new DtoToRadGroupCheckConverter());
    conversionService.addConverter(new DtoToRadGroupReplyConverter());
}
 
Example 5
Source File: SingleColumnRowMapperTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-16483
public void useCustomConversionService() throws SQLException {
	Timestamp timestamp = new Timestamp(0);

	DefaultConversionService myConversionService = new DefaultConversionService();
	myConversionService.addConverter(Timestamp.class, MyLocalDateTime.class,
			source -> new MyLocalDateTime(source.toLocalDateTime()));
	SingleColumnRowMapper<MyLocalDateTime> rowMapper =
			SingleColumnRowMapper.newInstance(MyLocalDateTime.class, myConversionService);

	ResultSet resultSet = mock(ResultSet.class);
	ResultSetMetaData metaData = mock(ResultSetMetaData.class);
	given(metaData.getColumnCount()).willReturn(1);
	given(resultSet.getMetaData()).willReturn(metaData);
	given(resultSet.getObject(1, MyLocalDateTime.class))
			.willThrow(new SQLFeatureNotSupportedException());
	given(resultSet.getObject(1)).willReturn(timestamp);

	MyLocalDateTime actualMyLocalDateTime = rowMapper.mapRow(resultSet, 1);

	assertNotNull(actualMyLocalDateTime);
	assertEquals(timestamp.toLocalDateTime(), actualMyLocalDateTime.value);
}
 
Example 6
Source File: TypeConversionConfiguration.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@ConditionalOnMissingBean
@Bean
ConversionService defaultCamelConversionService(ApplicationContext applicationContext) {
    DefaultConversionService service = new DefaultConversionService();
    for (Converter converter : applicationContext.getBeansOfType(Converter.class).values()) {
        service.addConverter(converter);
    }
    return service;
}
 
Example 7
Source File: SpringTypeConverterTest.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Bean
ConversionService camelSpringConversionService(ApplicationContext applicationContext) {
    DefaultConversionService service = new DefaultConversionService();
    for (Converter converter : applicationContext.getBeansOfType(Converter.class).values()) {
        service.addConverter(converter);
    }

    return service;
}
 
Example 8
Source File: NasServiceConfiguration.java    From radman with MIT License 5 votes vote down vote up
@Autowired
public NasServiceConfiguration(NasRepo nasRepo,
                               RadHuntGroupRepo radHuntGroupRepo,
                               DefaultConversionService conversionService) {
    this.nasRepo = nasRepo;
    this.radHuntGroupRepo = radHuntGroupRepo;
    this.conversionService = conversionService;

    conversionService.addConverter(new DtoToNasConverter());
    conversionService.addConverter(new NasToDtoConverter());
    conversionService.addConverter(new RadHuntGroupToDtoConverter());
    conversionService.addConverter(new DtoToRadHuntGroupConverter());
}
 
Example 9
Source File: SystemServiceConfiguration.java    From radman with MIT License 5 votes vote down vote up
@Autowired
public SystemServiceConfiguration(SystemUserRepo systemUserRepo,
                                  PasswordEncoder passwordEncoder,
                                  DefaultConversionService conversionService) {
    this.systemUserRepo = systemUserRepo;
    this.passwordEncoder = passwordEncoder;
    this.conversionService = conversionService;

    conversionService.addConverter(new SystemUserToDtoConverter(conversionService));
    conversionService.addConverter(new DtoToSystemUserConverter(conversionService));
    conversionService.addConverter(new AuthProviderToDtoConverter());
    conversionService.addConverter(new DtoToAuthProviderConverter());
    conversionService.addConverter(new RoleToDtoConverter());
    conversionService.addConverter(new DtoToRoleConverter());
}
 
Example 10
Source File: AccountingServiceConfiguration.java    From radman with MIT License 5 votes vote down vote up
@Autowired
public AccountingServiceConfiguration(RadAcctRepo radAcctRepo,
                                      DefaultConversionService conversionService) {
    this.radAcctRepo = radAcctRepo;
    this.conversionService = conversionService;

    conversionService.addConverter(new RadAcctToDtoConverter());
}
 
Example 11
Source File: PropertySourceConfigurationSource.java    From conf4j with MIT License 5 votes vote down vote up
private ConversionService getDefaultConversionService() {
    if (this.defaultConversionService == null) {
        DefaultConversionService cs = new DefaultConversionService();
        for (Converter<?, ?> converter : ((ListableBeanFactory) this.beanFactory).getBeansOfType(Converter.class, false, false).values()) {
            cs.addConverter(converter);
        }
        this.defaultConversionService = cs;
    }
    return this.defaultConversionService;
}
 
Example 12
Source File: GenericMapConverterTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelegatingConversion() {
	DefaultConversionService conversionService = new DefaultConversionService();
	GenericMapConverter mapConverter = new GenericMapConverter(conversionService);
	conversionService.addConverter(mapConverter);

	TypeDescriptor targetType = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(Charset.class),
			TypeDescriptor.valueOf(UUID.class));
	@SuppressWarnings("unchecked")
	Map<Charset, UUID> result = (Map<Charset, UUID>) conversionService
			.convert("UTF-8 = c46c18c7-b000-44d0-8326-f12ddf423972", targetType);

	assertThat(result, hasEntry(Charset.forName("UTF-8"), UUID.fromString("c46c18c7-b000-44d0-8326-f12ddf423972")));
}
 
Example 13
Source File: GenericMapConverterTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapes() {
	DefaultConversionService conversionService = new DefaultConversionService();
	GenericMapConverter mapConverter = new GenericMapConverter(conversionService);
	conversionService.addConverter(mapConverter);

	TypeDescriptor targetType = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class),
			TypeDescriptor.valueOf(String[].class));
	@SuppressWarnings("unchecked")
	Map<String, String[]> result = (Map<String, String[]>) conversionService.convert("foo = bar\\,quizz",
			targetType);

	assertThat(result.values().iterator().next(), Matchers.arrayContaining("bar", "quizz"));

}
 
Example 14
Source File: PlatformITBaseConfig.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public ConversionService conversionService() {
  DefaultConversionService defaultConversionService = new DefaultConversionService();
  defaultConversionService.addConverter(new StringToDateConverter());
  defaultConversionService.addConverter(new StringToDateTimeConverter());
  return defaultConversionService;
}