0

I am trying to implement converter in SpringMVC and configure it like in How to configure Spring ConversionService with java config? and some weird things happened.

ApplicationConfiguration.java

@Configuration
@EnableWebMvc
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry formatterRegistry) {
        formatterRegistry.addConverter(new StringToFoo());
        formatterRegistry.addConverter(new FooToString());
    }
}

I have a jsp with a form to pass a Foo object to controller. And I have <mvc:annotation-driven/> and component-scan in xml.

It turns out that the code I quoted was executed at init, but the converters were not called when passing from jsp to controller,

When I delete annotation-driven, everything works, converter was correctly called in my controller. However, before I added WebMvcConfigurerAdapter to the proejct, I had to include annotation-driven to init servlet.

So my questions:

What does annotation-driven do? Does WebMvcConfigurerAdapter (or the annotations used) have conflict with annotation-driven?

Community
  • 1
  • 1

1 Answers1

0

@EnableWebMvc is doing the same as annotation-driven, it is just in java instead of xml. When you have both it seems it gives the annotation-driven precedence and therefore the converters are not registered. Also see this question EnableWebMvc annotation meaning

Community
  • 1
  • 1
Björn
  • 1,593
  • 1
  • 13
  • 28