Example usage for org.springframework.format.support DefaultFormattingConversionService addFormatterForFieldAnnotation

List of usage examples for org.springframework.format.support DefaultFormattingConversionService addFormatterForFieldAnnotation

Introduction

In this page you can find the example usage for org.springframework.format.support DefaultFormattingConversionService addFormatterForFieldAnnotation.

Prototype

@Override
    public void addFormatterForFieldAnnotation(
            AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) 

Source Link

Usage

From source file:com.xpeppers.phonedirectory.config.WebAppConfig.java

private void addFormattersForFieldAnnotation(DefaultFormattingConversionService conversionService) {
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
    conversionService.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
}

From source file:de.asgarbayli.rashad.hbd.config.Config.java

@Bean
public FormattingConversionService conversionService() {
    // Create new one and disable defaults
    DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);

    // Activate @NumberFormat
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());

    // Activate @DateTimeFormat
    DateFormatterRegistrar dateFormatterRegistrar = new DateFormatterRegistrar();
    dateFormatterRegistrar.setFormatter(new DateFormatter("yyyy-MM-dd"));
    dateFormatterRegistrar.registerFormatters(conversionService);

    // Return new conversion service to use it as default
    return conversionService;
}

From source file:de.cpoepke.demos.neo4j.querydsl.config.SpringConfiguration.java

@Bean
public DefaultFormattingConversionService conversionService() {

    // Use the DefaultFormattingConversionService but do not register defaults
    DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);

    // Ensure @NumberFormat is still supported
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());

    // Joda Time Converter
    for (Converter<?, ?> converter : JodaTimeConverters.getConvertersToRegister()) {
        conversionService.addConverter(converter);
    }/*  ww  w.  j  a va  2s .  com*/
    conversionService.addConverter(new DateMidnightToStringConverter());
    conversionService.addConverter(new StringToDateMidnightConverter());
    conversionService.addConverter(new DateMidnightToLongConverter());
    conversionService.addConverter(new LongToDateMidnightConverter());

    // Register date conversion with a specific global format
    JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
    registrar.setUseIsoFormat(true);
    registrar.registerFormatters(conversionService);

    return conversionService;
}