Spring Formatter for field annotation not work in Web Flow

1.1k views Asked by At

Using spring web flow 2, Formatter by field type is effective but Formatter for Field Annotation is not.

getPrint and getParser not called. (by field type, they are called)

I've spent much time about this, but have no good results.

Bean for page

public TestBean {
    @TestFormat
    private String test;
    ...
}

Annotation

@Target({ElementType.TYPE,ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestFormat {}

AnnotationFormatterFactory

public class TestFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<TestFormat>,Serializable {
    @Override
    public Set<Class<?>> getFieldTypes() {
        Set<Class<?>> set = new HashSet<Class<?>>();
        set.add(TestFormat.class);
        return set;
    }
    @Override
    public Printer<?> getPrinter(TestFormat annotation, Class<?> fieldType) {
        return new TestFormatter();
    }
    @Override
    public Parser<?> getParser(TestFormat annotation, Class<?> fieldType) {
        return new TestFormatter();
    }
}

Formatter

public class TestFormatter implements Formatter<String>{
    @Override
    public String print(String str, Locale locale) {
        return str.substring(0, str.indexOf("parsed")); // example
    }
    @Override
    public String parse(String input, Locale locale) throws ParseException {
        return input + "parsed"; // example
    }
}

ApplicationFormatterRegistrar

public class ApplicationFormatterRegistrar implements FormatterRegistrar {
    @Override
    public void registerFormatters(FormatterRegistry registry) {
        registry.addFormatterForFieldAnnotation(new TestFormatAnnotationFormatterFactory());
    }
}

SpringMVC Configuration

<bean id="applicationConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatterRegistrars">
        <set>
            <ref local="applicationFormatterRegistrar"/>
        </set>
    </property>
</bean>
<bean id="applicationFormatterRegistrar" class="package.ApplicationFormatterRegistrar"/>

Spring Webflow Configuration

<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService" >
    <constructor-arg ref="applicationConversionService"/>
</bean>
<webflow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService"/>

this might be related, but I could not find a solution

Spring Web Flow 2.4.1
Spring 4.1.6
Thymeleaf 2.1.4
1

There are 1 answers

0
M. Deinum On

When implementing the AnnotationFormatterFactory then its getFieldTypes method should return the type of the fields the annotation applies to. With your current configuration you are saying TestFormat can be annotated with TestFormat.

I suspect however that you want to specify String can be annotated with TestFormat.

Change your implementation to return String.class instead.

public class TestFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<TestFormat>,Serializable {
    @Override
    public Set<Class<?>> getFieldTypes() {
        return Collections.singleton(String.class);
    }
    ...
}